# Problem with nesting for loops

**URL:** <https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832>\
**Category:** General\
**Created:** [August 31, 2016, 2:47pm UTC](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832 "2016-08-31T14:47:22Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![SerafimPoch](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/serafimpoch/32/39780_2.png) [@SerafimPoch](https://forum.freecodecamp.org/u/SerafimPoch)\
**Post date:** [August 31, 2016, 2:47pm UTC](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832/1 "2016-08-31T14:47:22Z")

</div>

![](https://global.discourse-cdn.com/freecodecamp/original/2X/a/a895c4dcd0e3a00051004cbc9dc94aa18d349801.png)

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&

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [August 31, 2016, 3:01pm UTC](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832/2 "2016-08-31T15:01:04Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SerafimPoch](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/serafimpoch/32/39780_2.png) [@SerafimPoch](https://forum.freecodecamp.org/u/SerafimPoch)\
**Post date:** [August 31, 2016, 3:15pm UTC](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832/3 "2016-08-31T15:15:41Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [August 31, 2016, 3:17pm UTC](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832/4 "2016-08-31T15:17:47Z")

</div>

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

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

---

<div class="post-metadata">

**Author:** ![SerafimPoch](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/serafimpoch/32/39780_2.png) [@SerafimPoch](https://forum.freecodecamp.org/u/SerafimPoch)\
**Post date:** [August 31, 2016, 3:20pm UTC](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832/5 "2016-08-31T15:20:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [August 31, 2016, 3:24pm UTC](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832/6 "2016-08-31T15:24:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![SerafimPoch](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/serafimpoch/32/39780_2.png) [@SerafimPoch](https://forum.freecodecamp.org/u/SerafimPoch)\
**Post date:** [August 31, 2016, 3:32pm UTC](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832/7 "2016-08-31T15:32:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kevcomedia](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevcomedia/32/125737_2.png) [@kevcomedia](https://forum.freecodecamp.org/u/kevcomedia)\
**Post date:** [August 31, 2016, 3:33pm UTC](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832/8 "2016-08-31T15:33:35Z")

</div>

Things become simple once you get it 😉

---

<div class="post-metadata">

**Author:** ![SerafimPoch](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/serafimpoch/32/39780_2.png) [@SerafimPoch](https://forum.freecodecamp.org/u/SerafimPoch)\
**Post date:** [August 31, 2016, 3:36pm UTC](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832/9 "2016-08-31T15:36:53Z")

</div>

It’s true. 🙂

---

<div class="post-metadata">

**Author:** ![ngocvuu](https://avatars.discourse-cdn.com/v4/letter/n/b782af/32.png) [@ngocvuu](https://forum.freecodecamp.org/u/ngocvuu)\
**Post date:** [September 1, 2016, 11:02am UTC](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832/10 "2016-09-01T11:02:44Z")

</div>

The best explanation ever 😃 thank you so much ^^

---

<div class="post-metadata">

**Author:** ![Tess105](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/tess105/32/18255_2.png) [@Tess105](https://forum.freecodecamp.org/u/Tess105)\
**Post date:** [November 24, 2016, 1:49am UTC](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832/11 "2016-11-24T01:49:23Z")

</div>

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 ?  
 ![](https://global.discourse-cdn.com/freecodecamp/original/3X/0/2/02d5bc6b2fc06431e487700760954bffad67a204.png)

---

<div class="post-metadata">

**Author:** ![NerdBurglur](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/nerdburglur/32/27356_2.png) [@NerdBurglur](https://forum.freecodecamp.org/u/NerdBurglur)\
**Post date:** [December 17, 2016, 6:42am UTC](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832/12 "2016-12-17T06:42:34Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![ParadiseArtist](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/paradiseartist/32/22268_2.png) [@ParadiseArtist](https://forum.freecodecamp.org/u/ParadiseArtist)\
**Post date:** [December 31, 2016, 8:03pm UTC](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832/13 "2016-12-31T20:03:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![hypersoniq](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/hypersoniq/32/63967_2.png) [@hypersoniq](https://forum.freecodecamp.org/u/hypersoniq)\
**Post date:** [December 31, 2016, 11:35pm UTC](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832/14 "2016-12-31T23:35:06Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![bjones2nd](https://avatars.discourse-cdn.com/v4/letter/b/74df32/32.png) [@bjones2nd](https://forum.freecodecamp.org/u/bjones2nd)\
**Post date:** [February 13, 2017, 5:14am UTC](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832/15 "2017-02-13T05:14:29Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![SerafimPoch](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/serafimpoch/32/39780_2.png) [@SerafimPoch](https://forum.freecodecamp.org/u/SerafimPoch)\
**Post date:** [February 13, 2017, 1:01pm UTC](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832/16 "2017-02-13T13:01:58Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ABitOfKylie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/abitofkylie/32/39654_2.png) [@ABitOfKylie](https://forum.freecodecamp.org/u/ABitOfKylie)\
**Post date:** [February 14, 2017, 12:24am UTC](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832/17 "2017-02-14T00:24:09Z")

</div>

@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: 5_6_7= 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.

---

<div class="post-metadata">

**Author:** ![bjones2nd](https://avatars.discourse-cdn.com/v4/letter/b/74df32/32.png) [@bjones2nd](https://forum.freecodecamp.org/u/bjones2nd)\
**Post date:** [February 14, 2017, 6:24am UTC](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832/18 "2017-02-14T06:24:22Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![bjones2nd](https://avatars.discourse-cdn.com/v4/letter/b/74df32/32.png) [@bjones2nd](https://forum.freecodecamp.org/u/bjones2nd)\
**Post date:** [February 14, 2017, 6:25am UTC](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832/19 "2017-02-14T06:25:12Z")

</div>

Thanks for taking the time to address this for me!

---

<div class="post-metadata">

**Author:** ![ABitOfKylie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/abitofkylie/32/39654_2.png) [@ABitOfKylie](https://forum.freecodecamp.org/u/ABitOfKylie)\
**Post date:** [February 14, 2017, 9:42pm UTC](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832/20 "2017-02-14T21:42:44Z")

</div>

🙂 Happy to help

[Next page](https://forum.freecodecamp.org/t/problem-with-nesting-for-loops/32832.md?page=2)
