Output Problem in Return Largest Numbers in Arrays

Tell us what’s happening:
When I logged the final test and see what’s going on, it turns out the output is [25,48,21,0] instead of [25,48,21,-3]. The rest of the tests are working just fine.

Why I’m getting 0 and not -3?

Your code so far


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

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);
console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]))

Your browser information:

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

Challenge: Return Largest Numbers in Arrays

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

Why I’m getting 0 and not -3?

Because your great variable starts at 0 :stuck_out_tongue:
I mean

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

results in

if (-3 > 0){
    great = -3
  }

But -3 is not greater than 0 so 0 will be pushed to the array instead^^

1 Like

Hmmm… didn’t know that. I changed the variable value from 0 to -3 directly and it got ticked off. Thanks, @Layer!!

1 Like