# Basic JavaScript - Nesting For Loops

Tell us what’s happening:
Describe your issue in detail here.
Can someone please describe how the math is done in this exercise? How is product * arr[i][j] calculated. I’m confused on everything in the arrays are mulitplied together.
Your code so far

``````function multiplyAll(arr) {
let 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 = product * arr[i][j];
}
}
// Only change code above this line
return product;
}

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

User Agent is: `Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36`

Challenge: Basic JavaScript - Nesting For Loops

Link to the challenge:

I’m going to take this question as you are asking what the value of `arr[i][j]` is each time through the inner loop.

Let’s assume that `arr` holds the following array:

``````[[1, 2], [3, 4], [5, 6, 7]]
``````

Forget about the `[j]` for a moment. If `i` is `0`, what is the value of `arr[i]`?

Its 2 correct right?

Is it? Explain your thoughts

[1, 2] is 0, [3, 4] is 1, and [5, 6, 7] is 2.

If what you mean is that `arr[0] = [1,2]`, `arr[1] = [3,4]`, and `arr[2] = [5,6,7]`, then yes, you are correct.

So when `i` is `0` then `arr[i]` is `[1,2]`. In other words, the `0`’th index in the array `[[1, 2], [3, 4], [5, 6, 7]]` is `[1,2]`.

So what is the `0`th index in the array `[1,2]`? This would be equivalent to `arr[i][j]` where both `i` and `j` are `0`. Do you see how that happens?

So is the function multiplying the indexes of i and j or the values inside of the array? So like 0*0 because of the index or is it 1 * 2 * 1 because the 0 index of i is [1, 2] and the 0 index of j is 1.

Let’s look at the code again:

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

You’ve initialized `product` to `1` before we begin the loops, so we know that the first time we get to that line, it is really going to be:

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

And since it is the first time through both the outer and inner `for` loop, `arr[i][j]` is really `arr[0][0]` which is `1`. So the very first time we hit that line it is really:

``````product = 1 * 1;
``````

So `product` still equals `1` after the first time through the inner loop.

But there are two elements in the first sub array, so while `i` is `0`, `j` will first be `0` (as described above) and then the second time through the inner loop it will be `1`:

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

We know that `product` is `1` at this point, so we can rewrite it as:

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

So what is the value of `arr[0][1]`? Based on that, what will the value of `product` be after the inner `for` loop is completed while `i` is `0`?

1 Like

Would it be

product = 1 * 1 * 2 ? index 1 of [j] is 2. So the value is 2

Correct, the value of `arr[0][1]` is `2` and so that line would be:

``````product = 1 * 2;
``````

Which means `product` will be `2` after going through the first sub array.

So based on what we just walked through, are you able to answer your original question now?

Since arr[i] is the outer array do we use the index value to find the product? So like say [i] is 1, so the [3, 4] index and say that [j] is on the fourth index which is 5. Would it be

product = 1 * 1 * 5
or
product = 1 * 1 * 4

The value of `product` isn’t multiplied by the values of `i` or `j`.

If `i` is `1` then `arr[i]` is `[3, 4]`. Since there are only two numbers in that array then `j` can only be either `0` or `1`.

``````product = 1 * 1 * 5
``````

This is never a possibility. The code is:

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

So there can only ever be two numbers on the right of the equals sign. The value of `product` will be whatever it currently is, and the value of `arr[i][j]` is the value retrieved from the array at those indices.

I think I understand now. So the first loop of [i] finds the values of [1, 2], [j] multiplies 1 and 2 and gives i [0] a value of 2. Next loop is the 3,4 which is 12, next is 5,6,7 which is 210. Then those values are multiplied together to get 5040.

I’m not quite sure you got it. The value of the items in the array never change. The only value that changes is `product` as you multiply it with new numbers.

Perhaps I did not read your explanation correctly? It would really help if you used correct syntax in your explanation.

And as @JeremyLT shows below, sometimes you just have to console log it out to see what’s going on

2 Likes
``````function multiplyAll(arr) {
let product = 1;
// Only change code below this line
for (let i = 0; i < arr.length; i++) {
console.log("i is", i);
console.log("arr[i] is", arr[i]);
for (let j = 0; j < arr[i].length; j++) {
console.log("i is", i);
console.log("j is", j);
console.log("arr[i][j] is", arr[i][j]);
console.log("product is", product);
product = product * arr[i][j];
console.log("product * arr[i][j] is", product);
}
}
console.log("final value of product is", product);
// Only change code above this line
return product;
}
``````
2 Likes

Thank you I can see it perfectly now!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.