Largest of four

Hola Coders,

I got stuck in the “Largest Numbers in Arrays” challenge. My code passed two of the tests. But in one of them, it repeats one of the largest number in the sub-array. Any tips/recommendations to correct this problem? ps:i know i can find a solution very easy on google, but i want to solve it by myself and a little help :wink:

function largestOfFour(arr) {
  
  // You can do this!
  var count=0;
  var arrNew=[];
  
  for(var i=0; i< arr.length;i++){
    
    
    for(var j=0; j<arr[i].length;j++){
      
      if(count < arr[i][j]){
               
      count=arr[i][j];
        
      }
     
    }  
    arrNew.push(count);
  }
  return arrNew;
}
//2ºTEST
largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

The reason this is happening is that when you start looking at the second array count is still the largest number from the first array. Because none of the numbers in the second array is larger than count it doesn’t change.

1 Like

Thanks for the tip. I’ve forced the count to reset to 0, at the end of iterating through each array, so only the biggest number of each sub-array(not of all sub-arrays), would be “push” in.