# Basic Javascript: Nesting for loops setting product

In this challenge, after we do our loops, we redefine product like so:

``````product *= arr[i][j];
``````

or

``````product = product * arr[i][j]
``````

How is it multiplying the items in the sub arrays together and then multiplying each of those products together as:

(1 * 2) * (3 * 4) * (5 * 6 * 7)

^Is this what is happening? If so, how? I don’t understand how we are telling it to do this.

Thank you for any help

You need to read most of JS code from right to left. The line `product = product * arr[i][j]` must be read as:

1. Code hits `*` operator: Take `arr[i][j]` and multiply to `product` and keep the result in memory
2. Code hits `=` operator: Point variable `product` to that place in memory

It’s very straight-forward and doesn’t ‘build’ a formula to calculate at the end. It just overwrites itself with every iteration, so correct representation would be:

``````LOOP1: product = 1 * 1
LOOP2: product = 1 * 2
LOOP3: product = 2 * 3
LOOP4: product = 6 * 4
...and so on
``````

yes this is really complicated I would recommend watching some youtube videos nested loops.
But I’ll try my best with words. remember when you count arrays they start/ index at 0.

Here is the question. You have 3 barrels of apples. Each barrel has several bags of apples inside the barrels. How many apples do we have in total?

Let’s write this as an array

## howManyApples([[1, 5,], [1, 8], [1, 4, 10]]) ; <------the[ ] are barrels and the numbers are bags of apples.

function howManyApples(countApples){
var apples = 0; <----- If multiplying set apples to 1 because 0 *0 is 0

for (var barrels=0; barrels < countApples.length; barrels++) {
for (var bags=0; bags < countApples[barrels].length; bags++) {

apples = apples + countApples[barrels][bags]; <—JS first look in barrels then look in bags.
}}
return apples;
}
howManyApples([[1, 2,], [1, 8], [1, 4, 10]]) ;

So let’s go through the loops as they count from [0], [1], [2] for the barrels, remember JS can only do 1 thing at a time.

remember apple is = 0 and every time this loops the value of apple will be updated to a new number.

apples = apples + countApples[barrels][bags]; <------ on the 1st loop we get the first barrel[0]
then bags[0] looks in the barrel and sees 1 bag. <-----apples = 0 +1 = 1

apples = apples + countApples[barrels][bags]; <------ on the 2nd loop we still get the first barrel[0] then bags[1] looks in the barrel and sees 2 bags. <----apples = 1 + 2 = 3

the length of the bags counter is = countApples[barrels].length…so this is the end of barrel[0].

apples = apples + countApples[barrels][bags]; <------ on the 3rd loop we get barrel[1] then bags[0] looks in the barrel and sees 1 bag. <----apples = 3 +1 = 4

apples = apples + countApples[barrels][bags]; <------ on the 4rth loop we still get barrel[1] then bags[1] looks in the barrel and sees 8 bags. <----apples = 4 + 8 = 12

end of loop barrel[1]

apples = apples + countApples[barrels][bags]; <------ on the 5th loop we get barrel[2] then bags[0] looks in the barrel and sees 1 bag. <----apples = 12 + 1 = 13

apples = apples + countApples[barrels][bags]; <------ on the 6th loop we still get barrel[2] then bags[1] looks in the barrel and sees 4 bags of apples. <----apples = 13 +4 = 16

apples = apples + countApples[barrels][bags]; <------ on the 7thth loop we still get barrel[2] then bags[2] looks in the barrel and sees 10 bags of apples. <----apples = 16 + 10 = 26

end of loop barrel[3]

returns apples.

and then if you wanted

alert(“you have” + apples + “apples!”);

1 Like

Thank you very much this is very thorough and helpful. I also found this video and the last 1/3 is also very helpful in explaining how the math is happening: https://www.youtube.com/watch?v=lphSLN210kg

2 Likes

Thank you I do understand now. Thanks again.