Why does my code not work for ALL examples?

Tell us what’s happening:
I’ve added a console.log() to see if my code works. It comes up with the correct result. However, when I run the official test to complete the excercise, I’m told it won’t work for all examples. I think it’s having a problem with negative numbers, but I’m not 100% sure about that. Where is the hiccup in my code?

  **Your code so far**

function largestOfFour(arr) {
var newArr = [];
for (var i = 0; i < arr.length; i++) {
  var largest = 0;
  for (var j = 0; j < arr[i].length; j++) {
    if (arr[i][j] > largest) {
      largest = arr[i][j];
    }
  }
  newArr.push(largest);
}
console.log(newArr)
return newArr;
}

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/100.0.4896.88 Safari/537.36

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

Hi @TheRev !

Welcome to the forum!

Well, let me ask you this, will zero ever be smaller than a negative number?

Once you fix that, then the test will pass. :slight_smile:

1 Like

Of course it won’t. Bummed I didn’t think of that myself. Thank you! I’ll start figuring out a work around.

1 Like

No worries.
That trips most people up :slight_smile:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.