Return Largest Numbers in Arrays (Code works but doesn't pass test)

Tell us what’s happening:

My code works but it still doesn’t pass the test. What’s wrong with it?

Your code so far



let newarr=[];
let highNo=-Infinity; //Number.NEGATIVE_INFINITY

function largestOfFour(arr) {
  for (let i =0; i<arr.length; i++){
    for (let j=0; j<arr[i].length; j++){
      if (arr[i][j]>highNo){
          highNo=arr[i][j];
      }
    }
    newarr.push(highNo);
    highNo=Number.NEGATIVE_INFINITY;
    //console.log(lowNo);
  }
  return newarr;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36.

Link to the challenge:

FCC tests doesn’t reset global variables between the tests.
You should move declaration of newarr inside the function.

Thanks Jenovs. That made it work. But in real world situations I am ok declaring it outside a function?

Generally you should avoid using global variables.