0
Hello, I’m a beginner in programming. I’m using the book for my studies in Javascript Eloquent Javascript. I do not understand the operation of the code below:
Code
var JOURNAL = [
{
"events":["carrot","exercise","weekend"],
"squirrel":false
},
...
];
function hasEvent(event, entry) {
return entry.events.indexOf(event) != -1;
}
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;
if (entry.squirrel) index += 2;
table[index] += 1;
}
return table;
}
console.log(tableFor("pizza", JOURNAL)); // → [76, 9, 4, 1]
JOURNAL
https://eloquentjavascript.net/2nd_edition/code/jacques_journal.js
My doubt
In general, I know that the function travels through the loop in search of the record that corresponds to the event determined in the argument. However, how it comes to this result is unknown to me and for once I did not find material to answer my questions.
JOURNAL
would be exactly what?– Francisco
It is an array. The complete code is here: https://eloquentjavascript.net/2nd_edition/code/jacques_journal.js
– GhostBC
Please ask the question.
– Francisco