# Basic Algorithm Return Largest Numbers in Arrays

**URL:** <https://forum.freecodecamp.org/t/basic-algorithm-return-largest-numbers-in-arrays/153183>\
**Category:** General\
**Created:** [October 14, 2017, 7:43pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-return-largest-numbers-in-arrays/153183 "2017-10-14T19:43:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dinhhuudinh](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dinhhuudinh/32/63862_2.png) [@dinhhuudinh](https://forum.freecodecamp.org/u/dinhhuudinh)\
**Post date:** [October 14, 2017, 7:43pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-return-largest-numbers-in-arrays/153183/1 "2017-10-14T19:43:01Z")

</div>

I don’t know the reason why i get wrong with this code!  
anyone can help me fix it and show me the errors here please ha!

```auto
function largestOfFour(arr) {
  var newarr =[];
  for(var i =0;i<4;i++){
    var largest = 0; 
    for( var j =0; j < arr[i].length;j++){
      if(largest < arr[j]){
       largest = arr[j]; 
    }
    newarr.push(arr[j]);
  }
      }
  return newarr;
}

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

```

---

<div class="post-metadata">

**Author:** ![3gimpfingers](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/3gimpfingers/32/68037_2.png) [@3gimpfingers](https://forum.freecodecamp.org/u/3gimpfingers)\
**Post date:** [October 14, 2017, 7:57pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-return-largest-numbers-in-arrays/153183/2 "2017-10-14T19:57:59Z")

</div>

You’re not accessing the subarrays. You have to use the format array[i][j] to access the subarray.

---

<div class="post-metadata">

**Author:** ![3gimpfingers](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/3gimpfingers/32/68037_2.png) [@3gimpfingers](https://forum.freecodecamp.org/u/3gimpfingers)\
**Post date:** [October 14, 2017, 8:00pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-return-largest-numbers-in-arrays/153183/3 "2017-10-14T20:00:26Z")

</div>

I believe it wants an array of values, one for each subarray.

---

<div class="post-metadata">

**Author:** ![MP7373](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mp7373/32/25433_2.png) [@MP7373](https://forum.freecodecamp.org/u/MP7373)\
**Post date:** [October 14, 2017, 8:01pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-return-largest-numbers-in-arrays/153183/4 "2017-10-14T20:01:06Z")

</div>

Instead of using arr[j], which is grabbing the whole sub array at index j, use arr[i][j] which is grabbing the value of index j in sub array i which is what you want.

So inside your inner loop:

for( var j =0; j \< arr[i].length;j++){

if (largest \< arr[i][j]) {  
largest = arr[i][j];  
}

newarr.push(arr[i][j]);

}

---

<div class="post-metadata">

**Author:** ![dinhhuudinh](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/dinhhuudinh/32/63862_2.png) [@dinhhuudinh](https://forum.freecodecamp.org/u/dinhhuudinh)\
**Post date:** [October 14, 2017, 8:08pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-return-largest-numbers-in-arrays/153183/5 "2017-10-14T20:08:12Z")

</div>

thanks for your help

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [October 14, 2017, 10:26pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-return-largest-numbers-in-arrays/153183/6 "2017-10-14T22:26:45Z")

</div>

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See [this post](http://forum.freecodecamp.com/t/forum-code-formatting/25574) to find the backtick on your keyboard. The “preformatted text” tool in the editor (`</>`) will also add backticks around text.

![markdown_Forums](https://sea3.discourse-cdn.com/freecodecamp/uploads/default/original/3X/4/b/4b9a26f0c243380dd84935f4e29b92e3ecee8d05.gif)

---

<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:37pm UTC](https://forum.freecodecamp.org/t/basic-algorithm-return-largest-numbers-in-arrays/153183/7 "2021-01-17T20:37:32Z")

</div>

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


