Can someone explain exactly what J is doing. And also can you breakdown the functioning of arr[i][j]?

Tell us what’s happening:

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

User Agent is: `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36`.

Challenge: Nesting For Loops

j represents the index of an element of one of the inner arrays contained in arr. For example, arr[1] references the inner array of [3, 4], so if you wanted to reference the 3 in the array, j would be 0.

``````var arr = [[1, 2], [3, 4], [5, 6, 7]];
console.log(arr[1][0]); // displays 3
``````

Variables are simply a named representation of some value. Naming things is useful for writing readable code.

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

In the example you gave where the argument `arr` is `[[1, 2], [3, 4], [5, 6, 7]]`, let’s look at what happens in the first iteration of the loops (ie `i` and `j` both are `0`) by evaluating from left to right.

The variable `product` is `1` so we get

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

The variable `arr` is `[[1, 2], [3, 4], [5, 6, 7]]` so we get

``````product = 1 * [[1, 2], [3, 4], [5, 6, 7]][i][j]
``````

The variable `i` is `0` so we get

``````product = 1 * [[1, 2], [3, 4], [5, 6, 7]][0][j]
``````

Now we can reduce the `[[1, 2], [3, 4], [5, 6, 7]][0]` bit down to `[1, 2]` because that’s the value at the 0-index.

``````product = 1 * [1, 2][j]
``````

The variable `j` is `0` so we get

``````product = 1 * [1, 2][0]
``````

Again, the 0-index of `[1, 2]` is `1` so we can reduce that bit down

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

Which is the same as

``````product = 1
``````

Then `j` is incremented and the loops continue from there exactly as above.

1 Like

This was incredibly helpful I really appreciate your very detailed explanation. Thank you very much.

Here’s another visualization just in case it helps. When you have an array of arrays, it’s common to call it a 2-dimensional array, and as you might imagine, we can display it as a 2-dimensional table.

``````i↓j→| 0 | 1 | 2 |
================|
0 || 1 | 2 |   |
----------------|
1 || 3 | 4 |   |
----------------|
2 || 5 | 6 | 7 |
``````

Sometimes it’s easier to structure your JavaScript in this way for readability.

``````const arr = [
[1, 2],
[3, 4],
[5, 6, 7],
];
``````
1 Like

i don’t understand how iteration works here, why j is incremented before i?
shouldn’t i be 1 in order to j be 1?

The inner loop is:

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

Which initializes `j` to 0, and runs until `j` is not less than `arr[i].length`.

I think now i get it , thank you so much.