How can I find and return the largest numbers from each subarray within a multidimensional array, resulting in an array of these largest numbers?
For instance: array = [[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]
The expected output would be: [ 5, 27, 39, 1001 ]
I attempted this solution:
function largestOfFour(arr) {
for(var i = 0; i < arr.length; i++) {
large = 0;
for(var j = 0; j < arr[i].length; j++) {
if(arr[i][j] > large) {
large = arr[i][j];
}
}
}
return arr.push(large);
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
However, only the largest number from the first subarray is being returned (5)