# Nesting For Loops: Why do you have to check the \[i\] length?

**URL:** <https://forum.freecodecamp.org/t/nesting-for-loops-why-do-you-have-to-check-the-i-length/279694>\
**Category:** General\
**Created:** [May 26, 2019, 5:34am UTC](https://forum.freecodecamp.org/t/nesting-for-loops-why-do-you-have-to-check-the-i-length/279694 "2019-05-26T05:34:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sidneyyin](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sidneyyin/32/116281_2.png) [@sidneyyin](https://forum.freecodecamp.org/u/sidneyyin)\
**Post date:** [May 26, 2019, 5:34am UTC](https://forum.freecodecamp.org/t/nesting-for-loops-why-do-you-have-to-check-the-i-length/279694/1 "2019-05-26T05:34:48Z")

</div>

I have a question about the Inner Loops lesson:  
[https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/nesting-for-loops](https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/nesting-for-loops)

For the inner For Loop, I used

```auto
for (var j = 0; j < arr.length; j++)

```

In theory, shouldn’t this also work, since arr.length is 3? The solution tells me I need to use `arr[i].length`

The solution they gave was:

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

```

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [May 26, 2019, 5:37am UTC](https://forum.freecodecamp.org/t/nesting-for-loops-why-do-you-have-to-check-the-i-length/279694/2 "2019-05-26T05:37:46Z")

</div>

But `arr[0]` is `[1,2]` - so you need to iterate over only two numbers, and `arr[0].length` is 2

`arr[0][2]`is `undefined`, I think the multiplication with this would result in `NaN` (Not a Number), so you would not get the result you want

---

<div class="post-metadata">

**Author:** ![sidneyyin](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/sidneyyin/32/116281_2.png) [@sidneyyin](https://forum.freecodecamp.org/u/sidneyyin)\
**Post date:** [May 26, 2019, 5:46am UTC](https://forum.freecodecamp.org/t/nesting-for-loops-why-do-you-have-to-check-the-i-length/279694/3 "2019-05-26T05:46:48Z")

</div>

Oh that makes sense. I thought it would only multiply however long the array is. Thank you!!

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 16, 2021, 12:59pm UTC](https://forum.freecodecamp.org/t/nesting-for-loops-why-do-you-have-to-check-the-i-length/279694/4 "2021-01-16T12:59:07Z")

</div>

Closed on January 16, 2021, 12:59pm UTC


