# Return Largest Numbers in Arrays (javascript)

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

So I’m almost there. But there is a mistake that I made in this excerice. But I don’t see it. I have seen the solution. But I don’t get it why I should do it like this. Can someone help me?

``````  **Your code so far**
``````
``````
function largestOfFour(arr) {
let max = 0;
let newarr = [];

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

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

if(arr[i][j] > max){
max = arr[i][j];
}

}

newarr[i] = max;

}
console.log(newarr);
return newarr;
}

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

``````
``````  **Your browser information:**
``````

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

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

When you don’t understand a challenge, try to work it out without code first then translate what you did into code.

Our challenge is to go through each group of numbers and find the largest number in that group.
We then want to return one group of all of the largest numbers that we found.

Here is how I would go through each group, as a human(not worrying about code yet)
here is the first one.

I would first look at the number 4 and say, that is the largest number I have seen so far so I am going to remember that.
Then I would move onto the number 5, and ask myself is 5 larger than 4?
Since the answer is yes, then I would keep track of the number 5 as being the largest.
I would continue that process until I run out of numbers in that group.

If you continue that process, then you will get the largest number of each group.
I know that you have already looked at the solution but I would try to translate what we just did as humans into code.
Hopefully that will help you understand this solution using two for loops and an if statement.

You can also practice running through one of the test cases slowly so you can better understand what this code is doing.

Which brings me to the bug in your code.
Take a close look at this line here

This is a problem because of this test case here

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

This one has negative numbers.
0 will never be smaller than a negative number.

That is where you need to focus your attention.
Once you fix that, then the test will pass.

Hopefully that helps!

2 Likes

Thank you @jwilkins.oboe for the help and your beautiful explantion!

I understand it I think. I need to declared the largestnumber variable in the for-lus and set it to the first number of the current group. So after it finished the first group it gets set to the first number of the next group. Correct?

Kind Regards
Sarah

Did you pass the tests? I think it sounds like you understand the problem.

It isn’t just that `max` is `0` it’s that it never gets “reset” so it keeps the largest number from the previous array. So if the next array doesn’t have a number that is larger than the largest number from the previous array it won’t work.

As you can see from `27` getting repeated twice instead of the number `5` being added.

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

But as said, setting it to `0` also means `arr[i][j] > max` will never be true if the array only contains elements that are negative numbers.

``````console.log(largestOfFour([[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]))
// [ 25, 48, 21, 0 ]
``````

As an aside, you can technically also initialize (or reassign) `max` to `-Infinity` (or `Number.NEGATIVE_INFINITY`) inside the outer loop.

1 Like

Yes, I totally understand now! I also passed the test. My variable “max” was declared outside the for loops and gave it a value of zero. So it didn’t reset and it didn’t work if there were negative numbers in the array. Thank you @lasjorg

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