Having Trouble With Return Largest Numbers in Arrays

Having Trouble With Return Largest Numbers in Arrays
0

#1

Here is my code:

function largestOfFour(arr) {
var newArr=[];
for(var i=0;i<largestOfFour.length;i++){
var num =0;
for(var j=0;largestOfFour[i].length;j++){
if(largestOfFour[i][j]>=num){
num=largestOfFour[i][j];
if([j]==largestOfFour[i].length){
newArr.push(num);
}
}

}

}
}

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

What is wrong with that? Why I have : TypeError: largestOfFour[i] is undefined?


#2

you’re using “largestOfFour” as your array name in your “largestOfFour” function. The array parameter you are passing in that you need to be looping over is “arr” not “largestOfFour”. Start by renaming “largestOfFour” to “arr”. You also have an error in your second loop that would cause an infinite loop since you don’t test to see that the increment is smaller than the length.

I think you are also pushing your array in the wrong scope.