Return Largest numbers in Arrays, cannot pass test

Return Largest numbers in Arrays, cannot pass test
0

#1

hey guys,

I have reached the return largest number checkpoint, I think i have got the code almost correct as it passes first and third test but it doesn’t seem to pass the second test. Please help me find my mistake.

function largestOfFour(arr) {
  // You can do this!
  var largest=0, nArr=[] ;

  for(i=0;i<arr.length;i++)
    {
      for(j=0;j<arr[i].length;j++)
        {
          if(largest<arr[i][j])
            {
              largest=arr[i][j];
            } 
        }
      nArr.push(largest);
    }
  return nArr;
}

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

:slight_smile: thanks guys.


#2

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.


#3

Look closely at the output you get from the test that fails. There’s a hint there. :wink:


#4

Ah yes,

the largest variable retained the largest valve from the second for loop, it needs to be reset at the beginning of the first for loop. Fantastic. thanks.

function largestOfFour(arr) {
  // You can do this!
  var largest, nArr=[] ;

  for(i=0;i<arr.length;i++)
    {
      largest =0;
      for(j=0;j<arr[i].length;j++)
        {
          if(largest<arr[i][j])
            {
              largest=arr[i][j];
            } 
          
        }
      nArr.push(largest);
    }
  return nArr;
}

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