Return Largest Numbers in Arrays (Project Help)

Return Largest Numbers in Arrays (Project Help)
0.0 0

#1

Every time I run this code it stores 27 twice in the second test, could anyone give me any advice on my code.

function largestOfFour(arr) {
// You can do this!
var myArray = [];
var max = 0;

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

      for(var j = 0; j < 4; j++){
  
  if(arr[i][j] > max){
    max = arr[i][j];  
  }
  
}

myArray.push(max);

}
  
return myArray;
}

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

#2

looks good except one small but very important thing, the reason it gives you [ 27, 27, 39, 1001 ], is because every time it runs thru the major loop it preserves the max value of the previous array, so for the first array the max was 27, when it gets to the second array nothing beats 27 so it keeps it, on the 3rd array there is a new max , 39, so it reassigns max to 39 and also on the 4th array there is a new max that it assigns,1001, had your 4th array had no numbers that were > 39 , then it would have kept it at 39.
Hopefully I haven’t given away too much and you can solve it from here.