Challenge: Return Largest Numbers in Arrays

Tell us what’s happening:
Is my algorithm correct? I need to help to complete my code.
Thank!

Your code so far


function largestOfFour(arr) {
var largest =[];
for(var i = 0; i < arr.length; i++){

  var max = 0;
  var newarr = arr[i];
  for(var j = 0; j < newarr.length; j++){
    if(newarr[j] > max){
      max = newarr[j];
    }
    if(j == 3){
      largest.push(max);
    }
  }
}
return largest;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) coc_coc_browser/85.0.134 Chrome/79.0.3945.134 Safari/537.36.

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

if(j == 3){
  largest.push(max);
}

Why did you add this? Why not push the maximum after you finish the j loop?

  var max = 0;

Do you know that the largest value will be at least 0? Is there perhaps a better guess for the maximum value? (Also, use let, not var, all over the place.)

It looks like you fixed the newarr[i] bug.