# Basic Algorithm Scripting - Return Largest Numbers in Arrays

Tell us what’s happening:
Describe your issue in detail here.
Hey everyone…I need help on this…someone please to give me feedback on my code so far and why the logic isnt working…Thanks a lot.

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

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

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

Challenge: Basic Algorithm Scripting - Return Largest Numbers in Arrays

A couple things catch my eye…

``````for(let i = 0; i <= arr.length; i++){
``````

Why are you using `<=` there? Remember that arrays are 0 indexed so the highest index will actually be 1 less than the length. Also, why do you have `i++` at the end of your loop, you already have it here? You have the same problems with your j loop.

``````if(arr[j] == Math.max(arr[j])){
``````

I don’t think this is doing what you think. First, don’t use `==` - you almost always want `===`. Next, is arr[j] what you want here? Remember that this is an array of arrays. Also, that’s not how Math.max works. You could use it here to simplify this code, but not like that.

``````        return largestArr.push(arr[j]);
``````

Also, remember that `return` exits the current function. Period. And what value does push return? Google “mdn array push” - that is a good habit to get into.

If I’m having trouble on something, I usually build it in steps. The first step might be to get my loops to work right and see if I can log out the values of the arrays, one by one.

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