# The nesting For loops

**URL:** <https://forum.freecodecamp.org/t/the-nesting-for-loops/465109>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [June 15, 2021, 9:25pm UTC](https://forum.freecodecamp.org/t/the-nesting-for-loops/465109 "2021-06-15T21:25:43Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Buttercup7315](https://avatars.discourse-cdn.com/v4/letter/b/aca169/32.png) [@Buttercup7315](https://forum.freecodecamp.org/u/Buttercup7315)\
**Post date:** [June 15, 2021, 9:25pm UTC](https://forum.freecodecamp.org/t/the-nesting-for-loops/465109/1 "2021-06-15T21:25:44Z")

</div>

**Tell us what’s happening:**  
Hi, I need help. I do understand the For loops, at least most of them. I am stuck at the lesson of The Nesting For Loops, I want to know all the hows and whys, so far, i do understand all the way to 5040 ( where’s it comes from and how to get there) , however, I do not understand where’s the ([5,1],[0.2,4,0.5],[3,9]) come from and how it should return 54? Thank you

```
  **Your code so far**

```

```js

function multiplyAll(arr) {
var 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 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36`

**Challenge:** Nesting For Loops

**Link to the challenge:**

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/nesting-for-loops)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [June 15, 2021, 9:27pm UTC](https://forum.freecodecamp.org/t/the-nesting-for-loops/465109/2 "2021-06-15T21:27:58Z")

</div>

What have you tried so far? That looks like the starter code.

In this challenge, you need to multiply together every number in every sub array to make one big product of all of those numbers.

---

<div class="post-metadata">

**Author:** ![Buttercup7315](https://avatars.discourse-cdn.com/v4/letter/b/aca169/32.png) [@Buttercup7315](https://forum.freecodecamp.org/u/Buttercup7315)\
**Post date:** [June 15, 2021, 9:32pm UTC](https://forum.freecodecamp.org/t/the-nesting-for-loops/465109/3 "2021-06-15T21:32:27Z")

</div>

Hi, sorry, i meant Nesting For Loops. `multiplyAll([[5,1],[0.2, 4, 0.5],[3, 9]])` should return `54` .  
Would you please tell me how to get to 54 and where’s the [0.2,4,0.5] come from?

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [June 15, 2021, 9:34pm UTC](https://forum.freecodecamp.org/t/the-nesting-for-loops/465109/4 "2021-06-15T21:34:04Z")

</div>

What do you get if you multiply together every single number in those nested arrays?

Answer: The product is 54

The `[0.2, 4, 0.5]` is just a sub-array that you’ve been given.

---

<div class="post-metadata">

**Author:** ![Buttercup7315](https://avatars.discourse-cdn.com/v4/letter/b/aca169/32.png) [@Buttercup7315](https://forum.freecodecamp.org/u/Buttercup7315)\
**Post date:** [June 15, 2021, 9:39pm UTC](https://forum.freecodecamp.org/t/the-nesting-for-loops/465109/5 "2021-06-15T21:39:09Z")

</div>

`multiplyAll([[1,2],[3,4],[5,6,7]])` should return `5040`  
1234567 (12=2 23=6 34=6x4=24 and so on) keep on going till 5040

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [June 15, 2021, 9:40pm UTC](https://forum.freecodecamp.org/t/the-nesting-for-loops/465109/6 "2021-06-15T21:40:44Z")

</div>

Yes. And the challenge wants you to do that for any nested arrays with nested loops.

---

<div class="post-metadata">

**Author:** ![Buttercup7315](https://avatars.discourse-cdn.com/v4/letter/b/aca169/32.png) [@Buttercup7315](https://forum.freecodecamp.org/u/Buttercup7315)\
**Post date:** [June 15, 2021, 9:44pm UTC](https://forum.freecodecamp.org/t/the-nesting-for-loops/465109/7 "2021-06-15T21:44:11Z")

</div>

Would you please break down the 1st and 2nd set of product\*=arr[i][j] from  
multiplyAll([[1,2],[3,4],[5,6,7]]);

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [June 15, 2021, 9:46pm UTC](https://forum.freecodecamp.org/t/the-nesting-for-loops/465109/8 "2021-06-15T21:46:04Z")

</div>

I don’t understand what you are asking. I’m not going to write out the solution for you.

`arr[i][j]` is how you index into nested arrays to get a single number. You need a loop over all sub arrays and a loop over all numbers in each sub array. You need to multiply each number together inside of that nested pair of loops.

---

<div class="post-metadata">

**Author:** ![Buttercup7315](https://avatars.discourse-cdn.com/v4/letter/b/aca169/32.png) [@Buttercup7315](https://forum.freecodecamp.org/u/Buttercup7315)\
**Post date:** [June 15, 2021, 9:51pm UTC](https://forum.freecodecamp.org/t/the-nesting-for-loops/465109/9 "2021-06-15T21:51:55Z")

</div>

```auto
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;
}

multiplyAll([[1,2],[3,4],[5,6,7]]);

```

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [June 15, 2021, 9:52pm UTC](https://forum.freecodecamp.org/t/the-nesting-for-loops/465109/10 "2021-06-15T21:52:44Z")

</div>

I’ve edited your post for readability and added spoiler tags. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (`</>`) to add backticks around text.

 ![Pre-formatted-text](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](https://forum.freecodecamp.org/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (’).

---

<div class="post-metadata">

**Author:** ![Buttercup7315](https://avatars.discourse-cdn.com/v4/letter/b/aca169/32.png) [@Buttercup7315](https://forum.freecodecamp.org/u/Buttercup7315)\
**Post date:** [June 15, 2021, 9:53pm UTC](https://forum.freecodecamp.org/t/the-nesting-for-loops/465109/11 "2021-06-15T21:53:53Z")

</div>

Sorry, I got it. Thanks

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [December 15, 2021, 9:54am UTC](https://forum.freecodecamp.org/t/the-nesting-for-loops/465109/12 "2021-12-15T09:54:09Z")

</div>

Closed on December 15, 2021, 9:54am UTC

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