Return Largest Numbers in Arrays Challenge

Hey there, I am not sure why my code is not working. Actually it passes 3/4 tests. Only for the last one it says, that the test is not passed. Can you please help?

This is my code:

function largestOfFour(arr) {
let largestNumbers = [];
for (let i=0; i<arr.length; i++) {
let maxnumber = 0;
for (let ii=0; ii<arr[i].length; ii++) {

if (arr[i][ii] > maxnumber) {maxnumber = arr[i][ii]};

}
largestNumbers.push(maxnumber);
}
  return largestNumbers;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

The only error-message that I am getting is:

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

Can you please help? I dont know what is wrong with my code. Many thanks :slight_smile:
Official Solution

Hi @andreasgrigoryan, welcome to the forum.

If you look at the latest array of the failing input:

[-72, -3, -17, -10]

You can see that they are all negative numbers.
Your code don’t really account for that as you have hardcoded the “initial” maxnumber as 0.

let maxnumber = 0;

So for the above array -3 is the “max number”, however you are returning 0.

You need to think of a way to make it work for both positive and negative numbers.
That’s all :slight_smile:

2 Likes

It makes sense now, many thanks! :slight_smile: