Challenge help Return Largest numbers in arrays help

Hi everyone. Any ideas what is wrong with my code for this challenge? I can’t work out what is wrong with it but it’s not passing the challenge

largestArray = [];
function largestOfFour(arr) {
  // You can do this!
  for(i=0; i<arr.length; i++){
        arr[i].sort(function (a,b) {
        return b - a;
        });
  largestArray.push(arr[i][0]);
}
return largestArray;
}

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

Move largestArray = []; inside the function.

1 Like

Thanks so much! But why does the largestArray being outside the function cause the code to fail?

Because FCC tester doesn’t reset global variables between runs. So your largestArray variable will have some value in it before second and third tests.

1 Like

@jenovs

Declaring the array with var outside of the function would make it a local variable right?

Would the code have worked then?

no it would be a global variable … but declared … but still would not be allowed to pass.
(global is anything outside function)

also look at his for loop … for(i =0 … this is not declared and would have a global …i… created … really really needs to use var (this will not be stopped from passing)

Makes sense. Thanks a lot.