The array#sort method is used, which compares values ββas strings, not numbers. The best solution uses array.prototype.map and Math.max.apply
function largestOfFour(array) { return array.map(function(arr) { return Math.max.apply(Math,arr); }); }); largestOfFour([[4,5,1,3],[13,27,18,26],[32,35,37,39],[1000,1001,857,1]]) => [5,27,39,1001]
You can also pass an array#sort function and do something like this
array.sort(function(a,b) { return a - b })
The pop() function removes and returns the last element of the array. In this case, it will be 1001.
source share