Return Largest Numbers in Arrays need advice

Tell us what’s happening:
I tried for multiple times and in multiple ways, but I still cannot get to the end of it. Someone give me some advice from their perspective.

Your code so far

function largestOfFour(arr) {
  // You can do this!
  var arrMod = arr;
  var arrNew = [];
  for(i=0; i<arrMod.length; i++) {
    for (j=0; j<arrMod[i].length; j++) {
      arrMod[i].sort();
 
     
    }     arrNew.push(arrMod[i][3]);
    
  }
   return arrNew;
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36.

Link to the challenge:
https://www.freecodecamp.org/challenges/return-largest-numbers-in-arrays

For some reason, after sorting , I wanted it to extract the last number in the array, which should be the largest, and add it to the arrNew, but for the last nested array instead of 1001, 857 is extracted.

The default sorting method is a lexical (alphabetical) sort. Look into the sort function here for how to make it do what you want it to.

1 Like