Can someone explain nested for loops?

So i know how loops work at least not untill nested ones, so the way i figured out this challenge https://www.freecodecamp.org/challenges/nesting-for-loops inner loop aka nested loop calculates numbers in nested arrays? As long as 0 is smaller than length of innter array which is 3? Can someone explain this in detail?

What is it that you dont get ?? Can you specify it??

I dont understand they way it really calculates nested loop aka those numbers inside nested arrays.

The best way to understand it is run the sample they give and watch the output, honestly. BUT…The 2-dimensional array is an array of arrays. So you have (in the sample) 3 arrays of length 2. The outer loop counts from 0 to 2 indexing each of the smaller arrays. The inner loop then counts from 0 to 1, referencing the members of the array defined by outer[x].

With that, if you’re given arr = [[1,2],[3,4],[5,6]]:

and write

for(x=0; x<arr.length; x++){
   for(y=0;y<arr[x].length;y++){
     console.log(arr[x][y]);
}}

What would you expect to get in the console (follow the code mentally or run it in codepen)?

I know how to write code, but im trying to understand how it calculates. We dont calculate it, the code does it in this case For loop does it. Its best to know as well how it works instead of just knowing how to write code.

how what calculates? The loop…or something in the loop?

How loop calculates nested loops, [1,2],[3,4],[5,6,7] and im confused at why it multiplies it automaticaly 12=2, 23=6,64=24,245=120…

Nevermind i undrstood why it multiplies it, its to do with product = products times arr[i][j]; but i dont understand at why it multiplies by using products = product times arr[…], if i use “plus” instead of “times” it will addition it (sorry english isnt my first lang).

I think I’m mis-understanding what you are looking for. I told you how the loops are “calculated” above. The multiplication is up to you…inside the inner loop (it doesn’t happen automatically, but you end up multiplying product by the element in the inner array). If this is not helpful, I apologize because I’m apparently not understanding your question.

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

I actually understood how it multiplies it but i wonder why it does it if we multiply product with element in inner array (why does it multiply numbers inside of nested arrays if we multiplied product with element in inner array). Im trying to be more clear here but explaining in details…

I don’t know if I understand your question.

But, the first for loop runs with the conditions set in the loop. But when it gets to the 0th index. It gets another loop in a code block. With a second parameter of conditions. So it runs the second loop as if there wasn’t a first loop. But with a scoped parameter var i equals the value given by the for loop. When it gets to the second for loop it will do whatever statement is in there. In this case it will multiply the product times the value of arr[0][0]… arr[[0][1] untill it ends its nested for loop. Then it increments var i = i + 1 and starts the second for loop again but now looks up the values in arr[1][0] etc.

1 Like

Ok i just watched this video https://www.youtube.com/watch?v=lphSLN210kg and he actually explained it really well, it actually took me at least 3 hours to understand everything. arr[i][j] starts like arr[0][0] which means first array of arr since there’s only three of them and “j” checked inside of that first array its numbers so “J” basically checked first number of nested array which in this challenge is 1 and then “j” moves to second number which is 2, and then second loop starts so it starts for second array inside arr and “J” checkes numbers inside of that array and moves on… Now i understand it completely, its actually quite easy but first time ever pain in the a** to understand lol.

Thank you very much i understand it now. Now im happiest person ever.

1 Like

Imagine a grid of squares, made up of rows and columns (like a chess board).

To iterate over one row, use a for loop. To iterate over every row, use nested for loops.

The inner loop iterates over each row. When it gets to the end, the program control returns to the outer loop. This iterates once. Then the inner loop iterates over the next row. This repeats until every square on the board has been covered.

In the outer loop, you define a variable i. This is the row number. The inner loop has a variable j. This is the column number. At every point in the iteration, there is a variable i and a variable j which pinpoints the exact square on the board.

1 Like