Hey guys! I’m currently going through book Eloquent JS and I can’t understand this code below:
function tableFor(event, journal) {
let table = [0, 0, 0, 0];
for (let i = 0; i < journal.length; i++) {
let entry = journal[i], index = 0;
if (entry.events.includes(event)) index += 1;
if (entry.squirrel) index += 2;
table[index] += 1;
}
return table;
}
console.log(tableFor("pizza", JOURNAL));
// → [76, 9, 4, 1]
You can look up JOURNAL here: https://eloquentjavascript.net/code/#4
Particularly I can’t understand this line
let entry = journal[i], index = 0;
I know that we reassign every object of the journal to entry , but what index=0 does? And every other index:
index += 2;
table[index] += 1;