Return Largest Numbers in Arrays - logic help

Tell us what’s happening:

So I cannot pass this test. The logic seems correct but I keep failing a single test:

largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]) should return [25, 48, 21, -3].

Not only that, but the only thing that is not passing of that test is the last index of the result array. It keeps returning “0”. I can set the re-assignment of the largestNumber in the first for loop to -100 and it works, but that becomes static and won’t pass if there are lesser -numbers in the array.

Can somebody point me in the right direction? am I missing something? is my logic off?

Your code so far


function largestOfFour(arr) {
  let largestNum = 0;
  let result = [];

  for (let i = 0; i < arr.length; i++){

    largestNum = 0;

    for (let j = 0; j < arr[i].length; j++){

      if ( arr[i][j] > largestNum ){
        
        largestNum = arr[i][j];

      }

    }

    result.push(largestNum);

  }
  
  return result;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays/

You don’t have to set largestNum to a hard-coded value. I suggest just setting it to the first value in the sub-array.

Thank you Ariel. Much appreciated.