Hey guys, I was having a lot of trouble understanding loops and arrays/objects, so I did the following line by line description to help myself learn these concepts. Hope it helps!
Weresquirrel Example - Detailed Run-Through of the function tableFor(event, journal)
function tableFor(event, journal) { // This part defines the function with parameters of event, which is the actual string(s) of the events in the arrays, and the parameter of journal, which is the large journal var
let table = [0, 0, 0, 0]; // Next is the table var within the function, an array of 4 numbers, in the sense of [no event/no squirrel, yes event/no squirrel, no event/yes squirrel, yes event/yes squirrel]
for (let i = 0; i < journal.length; i++) { // for loop counter i to journal.length, which is the number of entries in the journal itself
**let entry = journal[i], index = 0;** // let var entry journal[0]th entry, index beginning at 0 within each journal entry,
**if (entry.events.includes(event)) index += 1;** // if within the events Array in each journal entry includes the name of an event, such as “work”, add 1 to index. Note that index=0 is no event, so if there is an event, we add 1 in count to it, since the 2nd number in the table denotes yes event/no squirrel.
**if (entry.squirrel) index += 2;** // if the entry also has squirrel=true, then it adds 2 to the index
**table[index] += 1;** // the loop then adds 1 to the appropriate VALUE of that index property within the table
}
return table; // So, in the example in the book, this table returns [76, 9, 4, 1]
}
Let’s use the following journal data for the 4 possible event pairs, looking at the event called “carrot":
var JOURNAL = [
{“events”:[“exercise”,“weekend”],"squirrel”:false}, // no event, no squirrel
{“events”:[“carrot”,“weekend”,“touched tree”],“squirrel”:false}, // yes event, no squirrel
{“events”:[“nachos”,“brushed teeth”,“cycling”,“weekend”],"squirrel”:true}, // no event, yes squirrel
{"events”:[“carrot”,"brussel sprouts”],"squirrel”:true}, // yes event, yes squirrel
];
So, when we run function tableFor(“carrot”, JOURNAL), let’s go through each entry in JOURNAL through the code and see what happens.
Entry 1: {“events”:[“exercise”,“weekend”],"squirrel”:false} // no event, no squirrel
table = [0,0,0,0] - table[0]=0, table[1]=0, table[2]=0, table[3]=0, where index as defined below refers to the ORDER of the item in brackets, NOT the value of the item itself. This is crucial!
i = 0; i < JOURNAL.length, which is 4 entries, so i will go up to 3, a total of 4 iterations
entry = JOURNAL[0], index = 0; so we start at ORDER=0 of the 1st entry
If (entry.events.includes(“carrot”)) then index to add 1, since no event, so index remains = 0
If (entry.squirrel) then index to add 2 no squirrel, so index remains = 0
table[index] +=1, which is table[0] since index is still 0, so, we add 1 to the VALUE of the FIRST item in the table, thus:
return table, which is = [1,0,0,0]
Entry 2: {“events”:[“carrot”,“weekend”,“touched tree”],“squirrel”:false}, // yes event, no squirrel
table = [1,0,0,0] - table continued from after processing the entry above
i = 1; i < 4,
entry = JOURNAL[1], index = 0; so we start at ORDER=0 of the 2nd entry
If (entry.events.includes(“carrot”)) then index to add 1, since yes event, so index = 1
If (entry.squirrel) then index to add 2 no squirrel, so index remains = 1
table[index] +=1, which is table[1] since index is 1, so, we add 1 to the VALUE of the SECOND item in the table, thus:
return table, which is = [1,1,0,0]
Entry 3: {“events”:[“nachos”,“brushed teeth”,“cycling”,“weekend”],"squirrel”:true}, // no event, yes squirrel
table = [1,1,0,0] - table continued from after processing the entry above
i = 2; i < 4,
entry = JOURNAL[2], index = 0; so we start at ORDER=0 of the 3rd entry
If (entry.events.includes(“carrot”)) then index to add 1, since no event, so index = 0
If (entry.squirrel) then index to add 2 yes squirrel, so index +2 = 2
table[index] +=1, which is table[2] since index is 2, so, we add 1 to the VALUE of the THIRD item in the table, thus:
return table, which is = [1,1,1,0]
Entry 4: {"events”:[“carrot”,"brussel sprouts”],"squirrel”:true}, // yes event, yes squirrel
table = [1,1,1,0] - table continued from after processing the entry above
i = 3; i < 4, - final run through this loop
entry = JOURNAL[3], index = 0; so we start at ORDER=0 of the 4th and final entry
If (entry.events.includes(“carrot”)) then index to add 1, since yes event, so index = 1
If (entry.squirrel) then index to add 2 yes squirrel, so index +2 = 3
table[index] +=1, which is table[3] since index is 3, so, we add 1 to the VALUE of the FOURTH item in the table, thus:
return table, which is = [1,1,1,1]
So, from this function, we can see that when we run an string type variable as the event using our JOURNAL object variable, we will compile a table array of 4 values, each representing one of 4 outcomes of our event pairing with our squirrel outcomes.
edit @ivga80: