0
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; // what is going on here?
    if (hasEvent(event, entry)) index += 1;
    if (entry.squirrel) index += 2;
    table[index] += 1;
  }
  return table;
}

console.log(tableFor("pizza", JOURNAL));

For the above code - What is the commented section doing? Journal is an array of objects, each with two properties, the first of which is 'events' and contains an array, the second is a boolean. I can see that an object is being accessed and stored in entry for each loop through, but i don't know what the , index=0; is doing.

Solaxun
  • 2,732
  • 1
  • 22
  • 41
  • It's a [multiple `var` statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var) – Bergi Oct 26 '14 at 22:51

1 Answers1

2

The comma just allows multiple variables to be declared at once, without having multiple var statements.

var entry = journal[i], index = 0;

is equivalent to:

var entry = journal[i];
var index = 0;
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335