# Having Trouble With Return Largest Numbers in Arrays

**URL:** <https://forum.freecodecamp.org/t/having-trouble-with-return-largest-numbers-in-arrays/133507>\
**Category:** General\
**Created:** [July 2, 2017, 6:57am UTC](https://forum.freecodecamp.org/t/having-trouble-with-return-largest-numbers-in-arrays/133507 "2017-07-02T06:57:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![molu1994lp](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@molu1994lp](https://forum.freecodecamp.org/u/molu1994lp)\
**Post date:** [July 2, 2017, 6:57am UTC](https://forum.freecodecamp.org/t/having-trouble-with-return-largest-numbers-in-arrays/133507/1 "2017-07-02T06:57:12Z")

</div>

Here is my code:

function largestOfFour(arr) {  
var newArr=[];  
for(var i=0;i\<largestOfFour.length;i++){  
var num =0;  
for(var j=0;largestOfFour[i].length;j++){  
if(largestOfFour[i][j]\>=num){  
num=largestOfFour[i][j];  
if([j]==largestOfFour[i].length){  
newArr.push(num);  
}  
}

```
}

```

}  
}

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

What is wrong with that? Why I have : TypeError: largestOfFour[i] is undefined?

---

<div class="post-metadata">

**Author:** ![nhavar](https://avatars.discourse-cdn.com/v4/letter/n/c57346/32.png) [@nhavar](https://forum.freecodecamp.org/u/nhavar)\
**Post date:** [July 2, 2017, 8:10am UTC](https://forum.freecodecamp.org/t/having-trouble-with-return-largest-numbers-in-arrays/133507/2 "2017-07-02T08:10:42Z")

</div>

you’re using “largestOfFour” as your array name in your “largestOfFour” function. The array parameter you are passing in that you need to be looping over is “arr” not “largestOfFour”. Start by renaming “largestOfFour” to “arr”. You also have an error in your second loop that would cause an infinite loop since you don’t test to see that the increment is smaller than the length.

I think you are also pushing your array in the wrong scope.

---

<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 17, 2021, 8:49pm UTC](https://forum.freecodecamp.org/t/having-trouble-with-return-largest-numbers-in-arrays/133507/3 "2021-01-17T20:49:00Z")

</div>

Closed on January 17, 2021, 8:49pm UTC


