Your code reassigns the name newArray to an empty array on each iteration, and returns a newly assigned value at the end of the iteration but never does anything with the returned value.
You probably want something like.
function largestOfFour(arr) {
var theMaxes = [];
for(var i=0,j=arr.length;i<j;i++) {
theMaxes.push(Math.max.apply(null, arr[i])); //Add to array that exists outside of for-loop.
}
return theMaxes;
}
I’m on it since this morning, trying to find what’s wrong, I think i’ve not understood yet how “for loops” works.
In your solution everything is ok, you just have to change “j++” by “i++” and it’s ok!
As @Pethaf said, you reassign an empty array to newArray during each iteration of the for loop. Also, you only get one iteration of the for loop, because your return statement is inside it. That is why you get the first sub array of [5] returned, but nothing else.
To fix your existing code, move the newArray assignment to before the for loop and move the return newArray to just outside of the for loop, then you can do the following:
function largestOfFour(arr) {
var newArray = []; // outside for loop
for(var i=0; i < 4 ;i++){
newArray.push(Math.max.apply(null, arr[i]));
}
return newArray; // outside for loop
}