arrays - eloquent javascript correlation tableFor explanation -
i taking first steps in programming , stuck problem eloquent, in particular weresquirrel problem. here goes :
function hasevent(event, entry) { return 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; } return table; } console.log(tablefor("pizza", journal)); // → [76, 9, 4, 1] i understand first function hasevent, returns true if entry contains given event.
what can't grasp tablefor function. can't how function flows , how table gets values. example console.log(tablefor("pizza", journal)); , [76, 9, 4, 1]. how god's sake ?
the journal supplied book , looks :
var journal = [ {"events":["pizza","exercise","weekend"],"squirrel":false}, {"events":["bread","pudding","brushed teeth","weekend","touched tree"],"squirrel":false}, {"events":["carrot","nachos","brushed teeth","cycling","weekend"],"squirrel":false}, {"events":["brussel sprouts","ice cream","brushed teeth","computer","weekend"],"squirrel":false}, {"events":["potatoes","candy","brushed teeth","exercise","weekend","dentist"],"squirrel":false}, {"events":["brussel sprouts","pudding","brushed teeth","running","weekend"],"squirrel":false}, {"events":["pizza","brushed teeth","computer","work","touched tree"],"squirrel":false}, {"events":["bread","beer","brushed teeth","cycling","work"],"squirrel":false}, {"events":["cauliflower","brushed teeth","work"],"squirrel":false}, {"events":["pizza","brushed teeth","cycling","work"],"squirrel":false}, {"events":["lasagna","nachos","brushed teeth","work"],"squirrel":false}, {"events":["brushed teeth","weekend","touched tree"],"squirrel":false}, {"events":["lettuce","brushed teeth","television","weekend"],"squirrel":false}, {"events":["spaghetti","brushed teeth","work"],"squirrel":false}, {"events":["brushed teeth","computer","work"],"squirrel":false}, {"events":["lettuce","nachos","brushed teeth","work"],"squirrel":false}, {"events":["carrot","brushed teeth","running","work"],"squirrel":false} ...etc what understand event passed parameter , looks through array of objects in jounral see if it's present. how counting take place ?
if (entry.squirrel) index += 2; why +2 ? why not index += 3; or index += 4; ???
and why table[index] += 1; ???
for example first loop goes this, : console.log(tablefor("pizza", journal));
//flow i=0; from first line above in journal, pizza present.
if (hasevent(event, entry)) index += 1; so index incremented , becomes 1.it continues :
if (entry.squirrel) index += 2; squirel false, nothing happens index.if squirel found, why +2 ???
then table[index] += 1 can't understand point.
can please break down me ? helpful training.
thank in advance.
the table array count of entries meet different criteria. table[3] count of entries both entry.squirrel hasevent(event, entry) true. table[2] count entry.squirrel true, table[1] count hasevent(event, entry) true, , table[0] count neither true.
so logic index starts @ 0. if hasevent(event, entry) true, add 1 it. if entry.squirrel true add 2 it. result if both true, end adding 3. , if neither true don't add anything, it's still 0.
then add 1 table[index] increment counter.
Comments
Post a Comment