Nesting for loops - what's happening?

Nesting for loops - what's happening?
0

#1

Having trouble understanding what’s going on in this function per line. See comments below - please correct my logic and/or explain what’s going on.

``````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];

/* var i = 0; i < arr.length; i++
i = [1]; 1 < 3; i++
i = [2]; 2 < 3; i++
i = [3]; 3 < 3; end
1 * 3 = 3
j = [1]; 1 < 1; j++
j = [2]; 2 < 1; end
1 * 2 = 2

3 * 2 = 6

Is that correct? */
}
}
// Only change code above this line
return product;
}

// Modify values below to test your code
multiplyAll([[1],[2],[3]]);

``````

#2

I put two console.log statements into your code to illustrate what is going on. Run this code and you will see your logic is correct.

``````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++) {
console.log('i = ' + i + '; j = ' + j + '; arr[' + i + '][' + j + '] = ' + arr[i][j]);
console.log('product = ' + product + ' * ' + arr[i][j] + ' = ' + product*arr[i][j] + '\n');
product *= arr[i][j];
}
}
// Only change code above this line
return product;
}

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

The above code yields:

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

i = 1; j = 0; arr[1][0] = 2
product = 1 * 2 = 2

i = 2; j = 0; arr[2][0] = 3
product = 2 * 3 = 6
``````

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

Since product = 1, isn’t this technically 1 * i * j?

When i = 0 and j = 0, then it goes to the first array in multiplyAll which is 1. So it’s then product * i * j = 1 * 1 * 1? Likewise, if it were multiplyAll([1, 2], [2], [5, 10]]), then it would be: i = 0 => arr[1, 2] and j = 0 => 1, since 1 takes position 0 in array [1, 2]?

So then, product *= arr[i][i] => 1 * 1 * 2 * 1 = 2? Where the first 1 comes from product = 1, 1 * 2 comes from multiplying the array values of [1, 2], and the last 1 comes from j = 0 => 1 in [1, 2].

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

i = 1; j = 0; arr[1][0] = 2
product = 1 * 2 = 2

i = 2; j = 0; arr[2][0] = 3
product = 2 * 3 = 6
``````

Why does j = 0 for all instances; does it never iterate past 0?

#4

Because each subarray of arr only has one element. Since arrays are zero indexed, j will only be 0 which represents the only element in the subarray.

If the example call would have been:

``````multiplyAll( [ [1.2],[3,4],[5,6] ] );
``````

then you would have seen j values of 0 and 1 because each subarray contains 2 elements.

#5

Ah, I understand! However:

``````multiplyAll([[1],[2],[3]]);

i = 2; j = 0; arr[2][0] = 3
product = 2 * 3 = 6
``````

How does this = 6? If i = 2, then it’s referring to 3, but if j = 0, then it’s referring to 1. In that case, shouldn’t it be 1 * 3 * 1 (product * i * j)?

#6

How does this = 6? If i = 2, then it’s referring to 3, but if j = 0, then it’s referring to 1. In that case, shouldn’t it be 1 * 3 * 1 (product * i * j)?

Because it is not:

``````product * i * j
``````

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

i and j are referring to indices.

#7

In this exercise though, product = 1. So if i = 2 and j = 0, then it’s:

1 * 3 * 1 = 3 (given that multiplyAll([[1],[2],[3]]))

This is how I understand the exercise and I’m not sure what I’m not getting

#8

I go back to my previous reply to try to make it more clear what is happening during the original example of

``````multiplyAll([[1],[2],[3]]);
``````

The following are the variables’ values before, during, and after the for loop.

``````//before for loop
product = 1

// inside for outer for loop
// 1st iteration of outer for loop
i = 0
// inside inner for loop
j = 0;
arr[0][0] = 1
product = 1
product *= arr[i][j] // product *= arr[0][0] => product = 1 * 1 = 1
// after above statement
product = 1
// end of inner for loop

// 2nd iteration of outer for loop
i = 1
// inside inner for loop
j = 0
arr[1][0] = 2
product = 1
product *= arr[i][j] // product *= arr[1][0] => product = 1 * 2 = 2
// after above statement
product = 2
// end of inner for loop

// 3rd iteration of outer for loop
i = 2
// inside inner for loop
j = 0
arr[2][0] = 3
product = 2
product *= arr[i][j] // product *= arr[2][0] => product = 2 * 3 = 6
// after above statement
product = 6
// end of inner for loop

// end of outer for loop

// after both for loops have ended
product = 6
``````