I am taking my first steps in programming and i am stuck with this problem from eloquent, in particular with the weresquirrel problem. Here it goes :
functi
I'm going through this same book and I came across this discussion and thought I could contribute as well. I'm a beginner as well so please correct me if I'm wrong. Anyway, this is how I understand it:
First, we call the function tableFor
with two parameters "pizza"
(a string; one of the possible causes for Jacques to turn into a squirrel we have to check) and JOURNAL
(an object; it consists of the following properties: "events"
- possible causes - and "squirrel"
- whether Jacques turned into a squirrel that day or not). The JOURNAL
itself is an array consisting of multiple objects. Also, the "events"
property is an array as well.
The two parameters "pizza"
and JOURNAL
are then passed to the tableFor
parameters event
and journal
, so now "pizza"
is event
and JOURNAL
is journal
.
Since the end result has to be an array consisting of 4 elements so we could calculate the correlation later, we declare a variable to store this array:
var table = [0, 0, 0, 0];
Then we have to figure a way to make the values of the array to increment themselves when needed: for (var i = 0; i < journal.length; i++)
which means we are making a loop to go through every element of the journal
. i = 0
means we're currently looking for "pizza"
in the 1st element of the journal
array and then move on the the 2nd one. 3rd one etc. until the last element of the array, hence i < journal.length
.
var entry = journal[i], index = 0;
means that we take the n-th element of the array and assign it to the entry
variable, and define index
(the current index in the table
array) as 0.
Here we call another function hasEvent
:
if (hasEvent(event, entry)) index += 1;
if (entry.squirrel) index += 2;
function hasEvent(event, entry) {
return entry.events.indexOf(event) != -1;
}
which means we take the event
("pizza"
) and the entry
the 0th element of journal
array and check whether it contains the key word "pizza"
. If it doesn't contain it indexOf
willll return -1, if it does it'll return the first index at which the given element is present. In our 1st case, {"events":["carrot","exercise","weekend"],"squirrel":false}
, there's no "pizza"
, so indexOf
will return -1. This line if (hasEvent(event, entry)) index += 1;
asks whether hasEvent is true (whether indexOf
is not equal -1). In our case it is equal -1, so we don't add 1 to index
. And this line if (entry.squirrel) index += 2;
asks whether it's true that Jacques has turned into a squirrel (we can see that it's false), so we don't add 2 to index
. So our current index we're working with remains at 0. And now we increase it by 1 (table[index] += 1;
), so in the very first scenario we have the following result [1, 0, 0, 0].
It's also worth mentioning that in our table
array:
`index 0 = 00 (no pizza, no squirrel)
index 1 = 10 (pizza, no squirrel)
index 2 = 01 (no pizza, squirrel)
index 3 = 11 (pizza, squirrel)`
So depending on each case, we'll move from index 0 to index 1,2,3 or stay at index 0 and increase it by one depending on which conditions are met. I hope someone finds my explanation/understanding useful.