Nested for loops inner loop output

So i had to look at the solution my answer wasn’t far off, i just forgot to set product variable at the start and also i was thinking more like product = arr[i] * arr[j] which i myself know its weird writing it this way, thats why I looked it up.

now for the second(inner) loop if i console log it by itself this is what it returns
[ 1, 2 ]
[ 3, 4 ]
[ 1, 2 ]
[ 3, 4 ]
[ 1, 2 ]
[ 3, 4 ]
[ 5, 6, 7 ]

can anyone make sense of it for me, like why return number repeatedly and 5 6 7 just return once why is that?
I know what to do but i just want to understand the 2nd inner loop output. if someone would explain that would be great


function multiplyAll(arr) {
// Only change code below this line
let product = 1;
for(let i = 0; i < arr.length; i++){
 for(let j = 0; j < arr[i].length; j++){
   product *= arr[i][j];
 }
}

// Only change code above this line
return product;
}

multiplyAll([[1,2],[3,4],[5,6,7]]);
  **Your browser information:**

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

Challenge: Nesting For Loops

Link to the challenge:

Am I correct in thinking the console log looks like this?

console.log(arr[j])

If so:

So, outer loop runs 3 times (there are 3 elements in the array)

First element is an array of length 2, inner loop runs twice.
console.log(arr[0]) is [1,2]
console.log(arr[1]) is [3,4]

Second element is an array of length 2, inner loop runs twice.
console.log(arr[0]) is [1,2]
console.log(arr[1]) is [3,4]

Third element is an array of length 3, inner loop runs thrice.
console.log(arr[0]) is [1,2]
console.log(arr[1]) is [3,4]
console.log(arr[2]) is [5,6,7]

If instead you logged arr[i][j], the logs would be

1
2
3
4
5
6
7
1 Like

you lost me there, what is length 2?

it means that there are two items inside the array

Arrays have a property called length which tells you the number of items in the array, it’s how you are looping over them in your code

hey can you explain his explaination a little bit better i’m lost as to why the output of

console.log(arr[j]) is
[ 1, 2 ]
[ 3, 4 ]
[ 1, 2 ]
[ 3, 4 ]
[ 1, 2 ]
[ 3, 4 ]
[ 5, 6, 7 ]
why is it not just
[ 1, 2 ]
[ 3, 4 ]
[ 5, 6, 7 ]
isn’t that 3 outer loop and the inner loop extract the numbers whilst the 3 loops is running

why the repeating numbers as you said arr[0] would run over [1,2] etc etc why should the output repeat itself

The first array has 2 items, second has 2, third has 3.

2 + 2 + 3 is 7, not 3, each item in the array is also an array, and you are looping through the values of each of these sub-arrays in turn.


The outer loop goes through the values in the array

There are three values:

The first value (arr[0]) is [1,2]
The second value (arr[1]) is [3,4]
The third value (arr[2]) is [5,6,7]

Each time the outer loop runs, loop through the current value.

The current value is also an array, which is why you can loop through it.

The first value of the first array (arr[0][0]) is 1
The second value of the first array (arr[0][1]) is 2
The first value of the second array (arr[1][0]) is 3
…etc

The variable i goes 0, then the variable j goes 0, then 1.
The variable i goes 1, then the variable j goes 0, then 1.
The variable i goes 2, then the variable j goes 0, then 1, then 2.