Return Largest Numbers in Arrays. Please help

Return Largest Numbers in Arrays. Please help
0

#1

Can someone please tell me why my code will not work for the case at the bottom of my code? It works well for other test cases. The following is my code: function largestOfFour(arr) {
// You can do this!
var finalArray = [];
var largerNum = 0;

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

  else if (largerNum < arr[i][j]) {
    largerNum = arr[i][j];
  }
}
 
finalArray.push(largerNum);

}
return finalArray;
}

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

//Any help will be greatly appreciated!


#2

Hello! You must reset your largerNum variable after push it, after this fix it works :wink:

P.s.: Are you sure that first if is needed?
P.p.s. Sorry for my bad english xD


#3

Ahhhhh thank you so much! That went right over my head! :confused: Thanks for taking the time to help me! :smiley: