 # Help understanding how the multiplication happens in nested for loops

Hi all,

I feel like i am missing some basic understanding here on what the nested loop is doing below. Any help would be appreciated.

The code below will pass the challenge however i don’t understand how its achieving this. I thought arr[i][j] is providing the position of the array but that does not seem right if as it would be product = product * arr[i][J] and (product is 1).

``````function multiplyAll(arr) {
var product = 1;
// Only change code below this line
for(var i=0; i < arr.length; i++){
for (var j=0; j < arr[i].length; j++){
product *= arr[i][j]
}
}
// Only change code above this line
return product;
}

multiplyAll([[1,2],[3,4],[5,6,7]]);
``````

`product` starts at `1`, but each time this line is hit

the value of `product` is updated.

``````function multiplyAll(arr) {
var product = 1;
// Only change code below this line
for (var i=0; i < arr.length; i++) { // Loop over all subarrays
for (var j=0; j < arr[i].length; j++) { // Loop over all elements of subarrays
product *= arr[i][j]; // Update value of product
}
}
// Only change code above this line
return product;
}

multiplyAll([[1,2],[3,4],[5,6,7]]);
``````
1 Like

Amazing thank you! I can’t tell you how long i have stared at this!

When you have

a *= b

this means;

a = a * b

So, it is being updated in every loop.

1 Like