I've been tackling the challenges of Eloquent JavaScript at the Lycanthrope's Log, but I'm struggling to grasp a particular code snippet. Despite my efforts in checking values and testing with console.log, this piece of code still eludes me:
var JOURNAL = [
{"events":["carrot","exercise","weekend"],"squirrel":false},
{"events":["bread","pudding","brushed teeth","weekend","touched tree"],"squirrel":false},
{"events":["carrot","nachos","brushed teeth","cycling","weekend"],"squirrel":false},
// ...
]
function hasEvent(event, entry) {
return entry.events.indexOf(event) != -1; // verifying if event occurred or not
}
function tableFor(event, journal) {
var table = [0, 0, 0, 0];
for (var i = 0; i < journal.length; i++) {
var entry = journal[i], index = 0;
if (hasEvent(event, entry)) index += 1; // adding +1 to index if pizza happened
//object of arrays --> console.log(entry);
if (entry.squirrel) index += 2; // increasing index by 2 if squirrel is true
table[index] += 1; //<<-- This part is puzzling me
// console.log(index);
}
return table;
}
console.log(tableFor("pizza", JOURNAL));
// How are these values being calculated and added on indexes? Can someone help clarify this program for me?
If anyone can shed some light on how the values are processed and inserted into the array indexes, I would greatly appreciate it.