# Nesting for loops explanation needed

Tell us what’s happening:
Describe your issue in detail here.

I can step through a nested loop, but I don’t seem to be able to step through this nested arrays problem to see how the answer is derived. Does the j loop keep going until there are no more j values in that particular array index? Can someone please walk me through how this works?

``````  **Your code so far**
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]]);

``````
function multiplyAll(arr) {
let product = 1;
// Only change code below this line

// 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/101.0.4951.41 Safari/537.36`

Challenge: Nesting For Loops

Yes. When it has no more values to loop through, the loop control is passed back to the outer loop which increments by 1 and then the process begins again for the next value of `i` It keeps doing this pattern of looping until both `i` and `j` reach the specified stopping point as defined by the loop condition and then the loops terminate.

the loop for `j` is doing the work, the loop for `i` makes sure `j `keeps going… `j` is 1, then 2, then 3, etc… all the way up to but not including `arr[i].length` (because it is checking to see if it is still less than that. When it becomes equal to ` arr[i].length` the `j` loop has no more work to do and terminates.

here `j` is doing the calculation, and `i` is increasing the index each time through.

A close but imperfect analogy is an analog wall clock. As the seconds hand goes all the way through 0 to 60, then the minute hand becomes 1, then the second hand goes 0 to 60 again, then minute is 2. Except a clock keeps going and going until the battery or power is cut. Whereas the loops stop when they reach their defined limits. Which is stated in terms of the length of the array in this case.

You can create an infinite loop on purpose or by mistake, and it would crash the program or browser.

I hope that makes sense.

more detailed explanation:

Thank you! This was helpful! As a beginner, I am finding quite a bit of the instruction I am encountering is lacking the step through of how the code works. Recursion is a good example. Easy to understand with the step through, maddening without it lol.

Yes, totally. The challenges are designed to make you think and research. It’s a different (but I think better) way to learn. Because that’s how you’d do it if you were doing as a job… trying things, reading documentation, etc. A lot of it is about problem solving and developing logical thinking skills.

People can give you the answer, but at some point in the past they got that knowledge from reading a lot of documentation and then asking questions.

You’re on the right track. Recursion is hard.

Happy Coding!

2 Likes

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