Largest Numbers in Sub Arrays

*Tell us what’s happening:
Good morning Everyone,

I’m stuck on this algorithm. I wanted to solve it by using a for loop to sort out the sub arrays and use the pop function to take the biggest number at the end of each sub array. When I use the sort function the first 3 sub arrays sort but the last last one does not. Am i doing something wrong. I am open to ideas of solving this a different way.

Thank you in Advance for all the help

Ray

Your code so far

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

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [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/63.0.3239.132 Safari/537.36.

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

Thanks again Randell! sorry asking another noob question.

here is my solution and it returns the largest numbers in each subarray but its not letting me complete the algorithm. I cannot figure out what i’m doing wrong or missing.

function largestOfFour(arr) {
  // You can do this!
  
  for (var i = 0; i < arr.length; i++){
    
 arr[i].sort(function(a, b){
     return a<b;
   
   }) -(arr[i].splice(1));
    
  }
  return arr;
}

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

Thank you for giving me hints and nudging me in the right direction! I hope to one day contribute as you do. Sorry about not formatting the code.

below is the final answer and it works :).



function largestOfFour(arr) {
// You can do this!

for (var i = 0; i < arr.length; i++){

arr[i].sort(function(a, b){
return a<b;

});
arr[i].splice(1);
arr[i]=arr[i][0];

}

return arr;
}

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