JS Function:
function largestOfFour(arr) {
var biggest = 0;
var newArray = [];
for(var i = 0; i < arr.sort().reverse().length; i++){
for(var j = 0; j < arr[i].length; j++){
if(biggest < arr[i][j]){
biggest = arr[i][j]; // problem after this
//newArray.push(biggest); this will add the first element in each sub-array
// result [4,5,13,27,32,35,37,39,1000,1001]
}
}
}
return newArray;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
// Expected Output:
// [5, 27, 39, 1001]
I am having a hard time with this. How would I add or change anything to what I have to get the biggest number from each sub-array and add that number to newArray using for-loops?
for(var i = 0; i < arr.length; i++) { newArray.push(Math.max.apply(null, arr[i])); }