# Stuck on Return Largest Numbers in Arrays challenge - Help appreciated

So here is my code that doesn’t seem to work. Please be gentle!

``````function largestOfFour(arr) {
// You can do this!
let largestNumbers = [];
for (let i = 0; i < arr.length; i++) {
let biggestNumber = 0;
for (let j = 0; j < arr[i][j].length; i++) {
if (arr[i][j] > biggestNumber) {
biggestNumber = arr[i][j];
}
}
largestNumbers.push(biggestNumber);
}
return largestNumbers;
}
``````

So to walk through it…

• I’m declaring an empty array called largestNumbers that will hold the largest number from each sub array.

• The first for loop accesses the first level array.

• A variable called biggestNumber is created to hold the highest number found in the sub-array. Initialised to 0.

• Second for loop then loops through the sub-array.

• The if statement then checks if each number in the sub-array is greater than the value held in the biggestNumber variable. If it is, it updates biggestNumber to the value currently being compared. Once that loop is complete, the largest number in the sub-array should be the value in biggestNumber.

• Finally the value in biggestNumber is pushed to the empty array largestNumbers.

• Once all of the sub-arrays have been looped through, the array largestNumbers should have the 4 highest numbers.

• The largestNumbers array is then returned.

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

this is the issue. `j < arr[i][j].length` will never be true, so the loop doesn’t run, and then you are incrementing `i`, here, not `j`

That is one problem. Another is the i++ in that line - is that what you want to index there?

That will get the code basically working. But it will still fail for the last test - you haven’t taken into account the possibility that all the numbers are negative. I can think of two ways to handle that: 1) Does JS have a number that is the lowest possible number? 2) Do you really need to check the first number? Could you just call that the biggest number before you start and start checking with the second number?

1 Like

Thanks for you help Leah. I’ve corrected the `i++` to `j++`. How do you mean `j < arr[i][j].length` will never be true?

arr[i][j] is a number, not an array, so it doesn’t have a `length`, as such you will always get `j < undefined`

Are you sure you want `arr[i][j]` there? Compare with the other loop

Thank you for your help Kevin. I forgot about the negative numbers. Could I just declare `let biggestNumber` and not assign a value? Then whatever the first value in the sub-array is, be it negative or positive, would be assigned to the variable.

Oh of course! I’ve tweaked it so now the second loop statement looks to the `arr[i].length`. Unfortunately it’s still not working as I thought it would.

``````function largestOfFour(arr) {
// You can do this!
let largestNumbers = [];
for (let i = 0; i < arr.length; i++) {
let biggestNumber;
for (let j = 0; j < arr[i].length; j++) {
if (arr[i][j] > biggestNumber) {
biggestNumber = arr[i][j];
}
}
largestNumbers.push(biggestNumber);
}
return largestNumbers;
}
``````

Then you would compare a number and something that is `undefined`, can’t you think of an other way?

And `6 < undefined` and `6 > undefined` are both false

I just set the biggestNumber as the first value in the sub-array and it all worked! Thanks for you help with this Leah. You are a wonderful person!

thank you, but the first letter is a capital i

I can’t even get that right! I think it’s time for me to step away from the laptop and go to bed!

1 Like

Sure, that second option is probably the better one. You also could have assigned the largest number as negative infinity. JS does have an infinity number.

U got it just go over your answer