Javascript excercise help needed

Hey fellow FCC coders. I am currently going through Javascript excercises, but I am currently stuck on this example for the past 1hr. I just don’t understand the whole concept. I do understand the logic of for loops and arrays, but excercise as a whole…
Any Javascript masters can explain this excercise line by line??
Cheers!
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/nesting-for-loops

The exercise is just trying to trying to help you understand how you can access elements in nested arrays. When you are looping through the array `var arr = [ [1,2], [3,4], [5,6] ];` the first for loop takes the first element in the array (i.e. [1, 2]), however as this element is also an array, we have to cycle through this array (to get the individual numbers 1 and 2). This is why we have a for loop in a for loop.

What the code has to do is access every number in the array of arrays (i.e. 1,2,3,4,5,6) and multiply them together. Hope this helps, let me know if you’re still confused / need further clarification

what have you tried so far? what’s your code?

I wanted to understand the calculation process of [i],[j] to get the 5040.

ex) first execution is [i][j], 1x2 or something like that. Sorry, an absolute novice in JS

Looping through the numbers in the array `var arr = [ [1,2], [3,4], [5,6,7] ];`
i=0, j=0 ---- this accesses the first element in the first sub-array (i.e. 1)
i=0, j=1 ---- this accesses the second element in the first sub-array (i.e. 2)
— array[0].length = 2, so the condition i=0, j=2 doesn’t execute (because j is not < 2), so we exit the 2nd for loop and increment the first —
i=1, j=0 ---- this accesses the first element in the second sub-array (i.e. 3)
i=1, j=1 ---- this accesses the second element in the second sub-array (i.e. 4)
— again we exit the second loop, because (because j=2 is not < arr[1].length), so we exit the 2nd for loop and increment the first —
i=2, j=0 ---- this accesses the first element in the third sub-array (i.e. 5)
i=2, j=1 ---- this accesses the second element in the third sub-array (i.e. 6)
i=2, j=2 ---- this accesses the third element in the third sub-array (i.e. 7)
— again we exit the second loop, because (because j=3 is not < arr[2].length), so we exit the 2nd for loop —
— now we exit the first loop as i is not < arr.length* (i.e. 3 is not < 3)

At each step, we are multiplying the value by the variable product which is initialised as 1.
So:
var product = 1 // Declaring and Initialising product to 1.

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

product = 1 * 1 = 1
product = 1 * 2 = 2
product = 2 * 3 = 6
product = 6 * 4 = 24
product = 24 * 5 = 120
product = 120 * 6 = 720
product = 720 * 7 = 5040
product = 5040

Using Array’s .length property we can tell the for loop when to exit, without knowing how many elements are in the array. If you are still confused, have a look at the MDN page. Or google “Iterating over arrays using for loops in JavaScript”