Could anyone please explain to me the following code. Specifically, what the variables i and j have to do with the multiplyAll array. I can’t quite understand how they are related.
function multiplyAll(arr) {
let product = 1;
// Only change code below this line
for (let i = 0; i < arr.length; i++) {
for (let j = 0; j < arr[i].length; j++) {
product = product * arr[i][j];
}
}
// Only change code above this line
return product;
}
multiplyAll([[1, 2], [3, 4], [5, 6, 7]]);
I’m sorry if this question sounds stupid I’m just starting out with Javascript and after trying to figure it out for myself I still couldn’t fully understand.
arr is a two dimensional array so you are using to loops in order to grab every value. i will be the first index (used to grab each array out of arr) then j is used to loop through that array.
Try dropping a console.log(i, j); right under this line:
product = product * arr[i][j];
You’ll be able to see the values of i and j change this way.
Oh I think I get it now, thank you very much!
So the i is going over the outer level of the multiplyAll array one at a time (0-2) while j is doing the same on the inner level arrays (0-1, 0-1, 0-2). Is this correct?