How indexing order in an array works?

I was reading https://eloquentjavascript.net/04_data.html and I came across this function which return the results of two events occurring together or not into an array. while I understand the function I am confused about the indexing, for example how do you know that 9 in the array refers to no squirrel/ pizza 9 times. Thank you.

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]

Well from what I read in your code and the article is that this functions checks the journal for the occurrance of the event “pizza” in conjunction to there being a squirrel or not. And creates a table to visualize the following outcomes:

  • no squirrel and no pizza
  • no squirrel and pizza
  • squirrel and no pizza
  • squirrel and pizza

To fill this table it checks each entry of the journal using a for loop. The journal is an array of objects each having two properties, events and squirrel. The events are listed in an array, the squirrel property gives either a false or a true

For example

{
  events : ["pizza", "nuts", "touched tree"],
  squirrel : true
}

Now lets see what the code does exactly.

let entry = journal[i], index = 0;
if (entry.events.includes(event)) index += 1;
if (entry.squirrel) index += 2;
table[index] += 1;

Looking at this code step by step the following happens:

  • First it stores the current object (journal[i]) under the variable entry together with the a varible named index with a value of 0.
  • In the next line of code it uses the includes method to check if the events (entry.events) property of the object contains 'pizza'. If this results in a true, the value of index will increase with 1.
  • Then the if condition checks if there is a squirrel (entry.squirrel) if there is one it returns a true and the index value is increased by 2.
  • And last, it increases the value of the table at the position of index with 1.

Now lets apply this code to our possible outcomes

no squirrel and no pizza:
There is no pizza so our first if statement will do nothing. There is no squirrel so our second if statement will do nothing. Thus the value of index remains 0, meaning the value of table at the 0th position will increase with 1 -> table = [+1, 0, 0, 0]

no squirrel and pizza:
There is a pizza so our first if statement will execute index += 1. The value of index is now 1. Next the second if statement will do nothing since there is no squirrel. The value of index is 1 so the value at the 1st position will increase with 1 -> table = [0, +1, 0, 0].

squirrel and no pizza:
There is no pizza so our first if statement will do nothing. The second statement will result in a true since there is a squirrel. The value of index is therefore increased with 2. The last line of code will this increase the 2nd position of table with 1 -> table = [0, 0, +1, 0].

squirrel and pizza:
Now for the last option. The first line of code will result in the value of index to be increased by 1, the second line will result in the value of index to be increased by 2 more. Total 3. The last line of code will then increase the value of table at the 3rd position by one -> table = [0, 0, 0, +1]

So following this logic and answering your question, for the event of “pizza” the function returns:

  • [76, 9, 4, 1] There are 76 instances of no squirrel and no pizza
  • [76, 9, 4, 1] There are 9 instances of no squirrel and pizza
  • [76, 9, 4, 1] There are 4 instances of squirrel and no pizza
  • [76, 9, 4, 1] There is 1 instance of squirrel and pizza

Hope this cleared things up for you. Happy coding!

2 Likes

Dank uwel a million times Jannes, ever in Ams I will buy you a beer. :+1:

2 Likes