Unable to accept negative values in "Return Largest Numbers in Arrays"

Hi, guys!
I know that tons of people have already asked this question but I am stuck here and I can’t find anyone’s code that matches with mine. There is one but that person didn’t write the code right.
I just want to know what I am doing wrong because it won’t store -3 from the array[3][1] because my temp resets itself to 0 every time it goes back to array[i] loop. How can I make it accept -3?

function largestOfFour(arr) 
{
let newArr = [];
let temp;

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

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

  newArr.push(temp);
}

return newArr;
}

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

Thank you in advance!

You can think of this line

as guessing that the numbers in the sub-array are at least as big as 0. But as you have discovered, this is a bad guess.

A really good guess would be a number that you know is in the sub-array.

1 Like