# Basic Algorithm Scripting: Return Largest Numbers in Arrays

Basic Algorithm Scripting: Return Largest Numbers in Arrays
0

#1

My code seems to give the required output when I post it in the console, however it’s not passing any tests. Is there something wrong with what I’ve done here or…?

``````let myArr = [];
function largestOfFour(arr) {

for (var i = 0; i < arr.length; i++) {
let pushMe = 0;

for (var j = 0; j < arr[i].length; j++) {
if (j === 0) {
pushMe = arr[i][j]
}
else if (arr[i][j] > pushMe) {
pushMe = arr[i][j];
}
}
myArr.push(pushMe);
}

return myArr;
}
largestOfFour([[4, 9, 1, 3], [13, 35, 18, 26], [32, 35, 97, 39], [1000000, 1001, 857, 1]]);
``````

#2

Your code contains global variables that are changed each time the function is run. This means that after each test completes, subsequent tests start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

``````var myGlobal = [1];
function returnGlobal(arg) {
myGlobal.push(arg);
return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
var myLocal = [1];
myLocal.push(arg);
return myLocal;
} // reliable - always returns an array of length 2
``````

#3

I knew it would be something basic like that. Thanks for the push!