I have the following problem, my function accepts an array that contains 4 arrays, each element is a number. The functions must return the largest element of each array.
function largestOfFour(arr) {
var largest = [];
for (var i = 0; i < arr.length; i++) {
largest.push(arr[i].sort().pop());
}
console.log(largest);
return largest;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
Results:
Array [ 5, 27, 39, 857 ]
Apparently it works, but when I tried with the last array [1000, 1001, 857, 1], in which 1000 and 1001 are larger than 857 I'm getting 857. Why does it happen?