Nesting for loop

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

we have multiplyAll([[1],[2],[3]]), it means arr.length = 2
let’s try to count to i = 2
when i = 2 , then 2 < arr.length . It is impossible because two isn’t less than two.
Hence that array should be ([1],[2])

If you understand what i mean please reply me.

Indexing arrays is zero-based, but the length is not. So arr.length will be 3. And so it also runs for i == 2.

3 Likes