Can someone explain nested for loops?

Can someone explain nested for loops?
0.0 0

#1

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?


#2

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


#3

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


#4

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)?


#5

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#6

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.


#7

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


#8

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…


#9

Just to be clear, you have already solved the challenge, but do not understand how your code works? If so, post your actual code and we can explain what it is doing.


#10

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).


#11

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.


#12
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]]);

#13

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…


#14

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.


#15

I modified your code with some console.log statements.

function multiplyAll(arr) {
	var product = 1;
	// Only change code below this line
	for (var i = 0; i < arr.length; i++) {
	  console.log('start of inner for loop')
		for (var j = 0; j < arr[i].length; j++) {
		  console.log('before multiplication, product = ' + product);
		  console.log('i=' + i + ', j=' + j + ', arr['+i+']['+j+']=' + arr[i][j]);
			product *= arr[i][j];
			console.log('after multiplying product by ' + arr[i][j] + ' product = ' + product)
		}
	  console.log('end of inner for loop\n');
	}
	// Only change code above this line
	return product;
}

// Modify values below to test your code
multiplyAll([[1, 2], [3, 4], [5, 6, 7]]);

After running the modified code, you will see the following in your browser’s console (Ctrl+Shft+J). See if this helps you to better visualize what is going on.

start of inner for loop
before multiplication, product = 1
i=0, j=0, arr[0][0]=1
after multiplying product by 1 product = 1
before multiplication, product = 1
i=0, j=1, arr[0][1]=2
after multiplying product by 2 product = 2
end of inner for loop

start of inner for loop
before multiplication, product = 2
i=1, j=0, arr[1][0]=3
after multiplying product by 3 product = 6
before multiplication, product = 6
i=1, j=1, arr[1][1]=4
after multiplying product by 4 product = 24
end of inner for loop

start of inner for loop
before multiplication, product = 24
i=2, j=0, arr[2][0]=5
after multiplying product by 5 product = 120
before multiplication, product = 120
i=2, j=1, arr[2][1]=6
after multiplying product by 6 product = 720
before multiplication, product = 720
i=2, j=2, arr[2][2]=7
after multiplying product by 7 product = 5040
end of inner for loop

=> 5040


#16

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.


#17

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


#18

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.