Largest Numbers in Arrays where is the mistake :(

Tell us what’s happening:
Hi !
I’m stuck with my code, I think I found the mistake but i can’t solve it . :flushed:
As long as the numbers in the following array are bigger the code works, but if an array contains smaller numbers than the previous array the code seems to ignore it.
exemple :
[[17, 23, 25, 12], [25, 7, 34, 48], [4, -10, 18, 21], [-72, -3, -17, -10]]
will return [25, 48, 48, 48]

Your code so far


function largestOfFour(arr) {
let newArr = [];
let biggerNum = 0;
 for (let i = 0; i < arr.length; i++){  // on parcours le premier tableau
       for (let j = 0; j < arr[i].length; j++){  // on parcours le second tableau
           if(arr[i][j] >= biggerNum){
             biggerNum = arr[i][j];
           } 
       }
       newArr.push(biggerNum)
       console.log(newArr);
 }
 return newArr
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:79.0) Gecko/20100101 Firefox/79.0.

Challenge: Return Largest Numbers in Arrays

Link to the challenge:

This has to go between for loops. See if you can explain why?

1 Like

Also, 0 might be ‘too big’.

Hint

What happens when your array is full of negative numbers?

1 Like

If biggerNum was set with the value of the first item of the array under test it would always be a sufficient quantity.

Did you read the hint @jeremyLT provided? This will not always be the case. :slight_smile:

Whoops - I misread your reply. Don’t mind me. :sweat_smile:

This is correct, and a good approach to take (to catch all edge cases).

if you have a series of numbers

-72, -3, -17, -10
and you set biggerNum to -72
which is the first item in the array under test
not the first item in the muti dimentional array

this is what I do in my solution

I declare the varbiggerNum but i do not initialize it
rather I set it at the start of the inner loop
this is appropriate for any series of numbers

a good programmer Never hard codes any value

I know this, but I provided the hint so that @evan.hermier could work through the problem himself rather than having someone just give them the answer.

In general, freeCodeCamp is about helping people understand and learn about how to think about and solve problems with code.

but if you set it to zero what about negative numbers ?

thank’s for your help, i found the problem few minutes ago (after 4h ! :sweat_smile: )
if I understood correctly by declaring my variable outside my loop, it would reset it every time ,and it looked at the whole arr every time instead of the the arr[i] .
that’s why it would stop at 48 here .
so I moved it and locked it into my first loop like this

let biggerNum = arr[i][0]; 

i was about to come back with the answer but after few test with the console.log to make sure I understood the exercise , i found that let biggerNum = arr[i][1]; or arr[i][2] also validates the exercise . Maybe it’s stupid and I just need a break :sweat_smile:
but thank you for your help :slightly_smiling_face:

I think your explanation is close to what’s going on.
Consider this input:

Click here for an interactive repl

https://repl.it/repls/SevereSmugPyramid#index.js

In this case, 4 is the biggest number in the first array, 8 is the biggest number in the second array, 4 is the biggest number in the third array, and 16 is the biggest number in the fourth array. But we don’t see the second 4 because 4 < 8.

When you don’t reset biggerNum for each array, you end up keeping track of the largest number found so far.

That’s a great idea to play around with that value to see if you understand what’s going on. Using any value in arr[i] will work, but the question here is why. How does resetting biggerNum to a value from arr[i] fix the problem described above?

1 Like

Thank you. It’s starting to become clear to me !
for the second problem i think it’s clear too
by declaring biggerNum = arr[i][3] for exemple my condition would looks like

if(arr[0][0] >= arr[i][3]) // 13 >= 26
//or
if(arr[0][1] >= arr[i][3] )// 27 >= 26 !! 
//or
if(arr[0][2] >= arr[i][3]) // 18 >= 26 
//or
if(arr[0][3] >= arr[i][3]) // 26 >= 26 

and arr[i][4] would be undefined since my array has only 4 number
if it make sense :sweat_smile:

1 Like