 # Basic Algorithm Scripting: Return Largest Numbers in Arrays ?!

Hello I solved a problem on the page that is https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays

Even If My solution works correctly but freeCodeCamp compiler says your solution is wrong.

Here my code below

``````function largestOfFour(arr) {

var subMax = 0;
var max = 0;
var maxArrIndex = 0;

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

if(subMax > max){
max = subMax;
maxArrIndex = i;
}
}

return arr[maxArrIndex];
}

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

The test suite is correct in this case. Your answer doesn’t cover the case where you have all negative numbers, for example.

Take a look at what you are returning in the console.log below.

1 Like

Sorry Guys, What I have noticed that is I did not understand question.
What I understand is that function must return an array that contain largest number!

I corrected it. works fine.
My new solution is now:

``````function largestOfFour(arr) {

let result = [];
for(let i = 0; i < arr.length; i++){
let max = arr[i];
for(let j = 0; j < arr[i].length; j++){
if(arr[i][j] > max){
max = arr[i][j];
}
}
result.push(max);
}
return result;
}
``````

Thank you. I found my mistake

1 Like

Thank your for suggestion but, problem is not related with negative numbers.
Here the solution: Basic Algorithm Scripting: Return Largest Numbers in Arrays ?!

One of your issues was related to handling negative numbers. I’m glad you figured it out. Nice work!