# Basic Algorithm Scripting - Return Largest Numbers in Arrays

Tell us what’s happening:
Describe your issue in detail here.

When I use this algorithm for this problem, the console output [ 27, 27, 39, 1001 ] to the screen. Please can someone help me with where the problem is…

``````function largestOfFour(arr) {
let maxNum = arr[0][0];
let newArr = [];

for (let i = 0; i < arr.length; i++) {
for (let j = 1; j < arr.length; j++) {
if (arr[i][j] > maxNum) {
maxNum = arr[i][j];
}
}
newArr.push(maxNum);
}
return newArr;
}

let total = largestOfFour([[13, 27, 18, 26], [4, 5, 1, 3], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

console.log(total);
``````

User Agent is: `Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/113.0`

Challenge: Basic Algorithm Scripting - Return Largest Numbers in Arrays

let maxNum = arr[0][0];

Do you re-initialise your maxNum for each sub-array?

Try console.log this variable.
So that you can keep track for debugging.

1 Like

Your problem is that you’re not re-initializing the max number properly. The max number variable should be reset for every position in the first array (remember you’re iterating over an array of arrays.)

Second problem is that the maxNumber variable is being initialized as the first position of the first array, in your case 13. Every time you get a number below that, it wouldn’t process. So you’ll get, for the second array, a value of 13, when the correct is 5. If you set it to 0 instead, your algorithm will work.

Mod Edit: SOLUTION REMOVED

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.

Thanks guys, for you help.

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