 # Iteratig through nested for loops // procedure

Dear coder,

again, I my brain stucks Code:

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;

}

// Modify values below to test your code

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

Can someone explain the procedure of this code? As I mentioned before, solving the tasks often don`t makes people more wise…

product *=  okay, understood.

product *=  sorry, don`t get it.
How comes  is executed by this function?

Best regards

``````// Modify values below to test your code
multiplyAll([[1,2],[3,4],[5,6,7]]);
``````

Say you have an array like `arr = [1,2,3]`

how would you get the first item in the array?
you would say `arr` and that would get the first item.

so `arr returns 1` and `arr returns 2` …and so on right?

what if you array was now `arr = [1,,3]`
we would get the second item by saying `arr` just like before

…but now `arr returns `

`` is also an array.

cool.

what if you array was now `arr = [1,[9, 7, 5],3]`

how would you get the first item in the nested array at `arr`

`arr returns [9,7, 5]`
`arr returns 9`

easy peasy

1 Like

Okay, understood.

My mind can`t figure out the following point:

function multiplyAll(arr) {

var product = 1;

for (var i = 0; i < arr.length; i++) {

for (var j = 0; j < arr[i].length; j++) {

product *= arr[i][j];

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

1. i = 0; j = 0 --> arr --> product *= 1
2. i = 1; j = 1 --> arr --> product *= 3

I can`t explain my self how the function will procede arr.
Will be the inner loop be executed till end, until the outer loop changes to i = 1 ?
If so, arr.length is 3, but the arr has just 2 values. So j = 2 or 3 leads to what?

Best regards

``````i is 0 so arr is 1,2
j is 0
arr is 1 so multiplying product by 1
j is 1
arr is 2 so multiplying product by 2

i is 1 so arr is 3,4
j is 0
arr is 3 so multiplying product by 3
j is 1
arr is 4 so multiplying product by 4

i is 2 so arr is 5,6,7
j is 0
arr is 5 so multiplying product by 5
j is 1
arr is 6 so multiplying product by 6
j is 2
arr is 7 so multiplying product by 7``````
``````for (var i = 0; i < arr.length; i++) {
// arr[i] returns arr, arr, arr, etc

for (var j = 0; j < arr[i].length; j++) {
// when i = 1, arr[j] returns arr, arr, arr, etc

// for every number arr[i][j] returns, perform a task

product *= arr[i][j];
// same as product = product * arr[i][j]
}
}
``````