Largest numbers in arrays challenge

Tell us what’s happening:
I was taking return largest numbers in arrays challenge in javascript algrorithms.
My code doesn’t seem to work for only one of the test cases and I cannot figure out why.

Your code so far


function largestOfFour(arr) {
let answer=[];
for(let i=0;i<arr.length; i++){
  let max = 0;
  for(let j=0; j<arr[i].length; j++){
    if(arr[i][j] > max){
      max = arr[i][j];
    }
  }
 answer.push(max);
}
return answer;
}

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; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36.

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

The failing test says you have trouble with negative numbers. Why are you guessing max = 0? 0 can be very big compared to your numbers : )

1 Like