# 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
Official Solution

2 Likes

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

2 Likes

It makes sense now, many thanks!

The solution can be simplified thus:

``````function largestOfFour(arr) {
let largest = [];
for (let i = 0; i < arr.length; i++) {
largest.push(Math.max(...arr[i]))
}
return largest;
}
``````

Iâ€™m new to the forum. Donâ€™t know if Iâ€™m breaking any rules.

1 Like

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original posterâ€™s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.