Eloquent Javascript 2nd. Chapter 4. Computing Correlation. Final Analysis - [Variable Recursion & in operator] Pt 2 -
Eloquent Javascript 2nd. Chapter 4. Computing Correlation. Final Analysis - [Variable Recursion & in operator] Pt 2 -
i'm working through end of first illustration in chapter 4 eloquent javascript. here total piece of code (it's lastly piece have questions regarding attached first portion reference).
var journal = []; function addentry(events, diditurnintoasquirrel) { journal.push({ events: events, squirrel: diditurnintoasquirrel }); function phi(table) { homecoming (table[3] * table[0] - table[2] * table[1]) / math.sqrt((table[2] + table[3]) * (table[0] + table[1]) * (table[1] + table[3]) * (table[0] + table[2])); } function hasevent(event, entry) { homecoming entry.events.indexof(event) != -1; } function tablefor(event, journal) { var table = [0, 0, 0, 0]; (var = 0; < journal.length; i++) { var entry = journal[i], index = 0; if (hasevent(event, entry)) index += 1; if (entry.squirrel) index += 2; table[index] += 1; } homecoming table; } function gathercorrelations(journal) { var phis = {}; (var entry = 0; entry < journal.length; entry++) { var events = journal[entry].events; (var = 0; < events.length; i++) { var event = events[i]; if (!(event in phis)) phis[event] = phi(tablefor(event, journal)); } } homecoming phis; } var correlations = gathercorrelations(journal); console.log(correlations.pizza);
my questions are:
what purpose of .events in
var events = journal[entry].events;
does phone call on recursion? if why? couldn't have had journal[entry] , function run calling on entry tablefor function? phone call addentry function (where events variable established) in of import way?
what purpose of(!(event in phis))
. i read : if event in phis true flip not true , trigger necessary phi calculation. wouldn't create more sense eliminate ! (does not equal) or piece of code altogether? if have loop won't function run on it's on until max length of journal , stop?
var events = journal[entry].events;
getting events
object object @ index entry
in array journal
, assigning temporary variable called events
.
this done convenience don't have maintain referring journal[entry].events
. example, later on has line:
var event = events[i];
which become:
var event = journal[entry].events[i];
without assignment temporary variable.
if(!(event in phis))
it's testing see if object phis
not have property name contained in variable event
. if object doesn't have property, adds next line:
phis[event] = phi(tablefor(event, journal));
see in operator
here's simple snippet of code help understand in
operator:
var foo = {}; console.log("bar" in foo); // logs "false" because foo doesn't have "bar" property foo.bar = 1; // or foo["bar"] = 1; console.log("bar" in foo); // logs "true" because foo has "bar" property
javascript
Comments
Post a Comment