# Problem with nesting for loops

Problem with nesting for loops
0

#1

I’ve been working on some exercises on freecodecamp and I am stuck on this nesting for loop exercise. I was able to find the solution, but I don’t quite understand.

Can someone explain to me how this work&

Need help understanding - Nesting For Loops
#2

The outer for-loop is for iterating through each subarray.
`i` is the index of the current subarray.

The inner for-loop is for iterating through each number in a given subarray.
`j` is the index of the current number in a given subarray

You refer to a subarray with `arr[i]`. Then you refer to an element in that subarray with `arr[i][j]`.

I don’t know if this is what you’re asking about though. Correct me if I’m wrong.

#3

in previous chapter
var myArr = [ 2, 3, 4, 5, 6];
for (var i = 0;i < arr.length;i++)
means - 2+3+4+5+6.
here the same in the first.
where it came from 5040?
I find it difficult at all to understand how it works (for) part 2
just not enough sleep

#4

It’s pretty much the same as the first one, except you’re multiplying.

1 * 2 * 3 * 4 * 5 * 6 * 7 = 5040

#5

It can not be so simple
Then why I write J ?

#6

The problem is that you don’t have one array filled with numbers. Instead you have one array filled with arrays of numbers. Before you can get to those numbers, you have to get to the subarrays first. That’s what the outer for-loop is for. The outer for-loop iterates through each subarray. Then in that for-loop you write another for-loop for accessing the numbers in that subarray.

#7

Thank you so much. I almost fell into a depression from the fact that I do not understand the seemingly simple task.

#8

Things become simple once you get it

#9

It’s true.

#10

The best explanation ever thank you so much ^^

#11

This lesson is a bit hard to figure out, please give me a hand~~

If I remove the three line about the product, it still work, than what is the reason to put it ?

#12

Where does the i++ and j++ factor in? Why are we adding 1 to [i] and [j] … is that how it tells the computer to cycle through the array and multiply by 1 ?

#13

I’m still trying to figure this out myself but I think I understand the piece that you are confused about. The i++ and j++ are so that it moves through the numbers in the subarrays of the array. Please someone correct me if I’m wrong or help add to my explanation and discussing it would help me to better grasp the concept.

#14

Predictability and good coding practice.
It is being initialized without declaring it if you cut line 3, but it seems to be good practice to delcare variables.
`var product = 1;` is 1
`var product;` is undefined
Your code works without it because after line 3, the next time product is used is with an assignment statement
’product = product * arr[i][j];`
If I am understanding JS so far, that means product is being hoisted out of your function and declared in the global scope by the compiler if you omit the declaration on line 3. Easier just to keep it there to avoid any unexpected results.

#15

Even after reading this thread over and over, I still can’t wrap my head around this. Smh. Will continue to visit this challenge until i grasp the concept or maybe future challenges will help me understand this one better. Thank you to everyone who posted!

#16

It is very simple to understand if you don’t think how it works For loop just extracts some items from array. At this time very little group of programmers use for loop.Try instead- array.map( ).It is the same for loop but with less code and problems.

#17

@bjones2nd Here comes a verbose explanation. Say that the first array “Big Box” (i loop) contains 3 arrays (blue array [1,2], yellow array [3,4], red array [5,6,7]). The first for loop is telling you to start with the Big Box and take each item one by one - so it takes the first array (blue) then continues to the j loop(innerloop) for
further instructions: product= product*arr[ i ][ j ];
which say: now I want you to multiply all the numbers [ 1, 2] inside the current, this case blue, array which belongs to the array Big Box [ i ] . Then when you’re done update the value of the variable called “product” with that result. Ok - got it so … blue 1x2 = 2. and now the new value for product will be 2. job done, oh time to run the i for loop again, now taking the yellow box … 3x4 = 12. ok and times the new product value of 2 - we now have 24. loop again: 567= 210, ok now multiply x 24 and voila you get 5040. Hopefully, this helps. if not - welcome to the inner workings of my scattered mind.

#18

I must have a scattered mind also, because this here actually makes the most sense by putting it into a more understandable perspective for me. Thanks a lot! I’m sure others visiting this post in the future will benefit from it as well.

Great contribution!

#19

Thanks for taking the time to address this for me!

#20

Happy to help