Largest Numbers in Arrays

My solution works but FreeCodeCamp did not accept my answer. I tested it on all parameters required. It produces the result in the array format that it is requesting. Please help

var largestArr = [];
function largestOfFour(arr) {
// You can do this!
for (var i = 0; i < 4; i++) {
arr[i] = arr[i];
// console.log(arr[0]);
// console.log(arr[1]);
// console.log(arr[2]);
// console.log(arr[3])
// console.log(arr[i][0]);;
arr[i].sort(function(a,b){
return a < b;
});
console.log(arr[i][0]);

``````  	console.log(largestArr);
}
console.log(largestArr);
return largestArr;

}

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

You are setting `largestArr` outside the function and you are not clearing it for every function call. So your function will only work correctly once.

1 Like

what does it mean when you say ‘not clearing it for every function call’ ?

the easiest way to fix your problem is by placing the `var largestArr = [];` inside the function. But you could also clear the array / empty it:

``````var largestArr = [];
function largestOfFour(arr){
largestArr = [];  // empty it
...
}
``````

This way `largestArr` is emptied every time you run the function.

1 Like