# Largest Array issue

Me again haha. I would love to know why my console.log is coming out with just [4, 5]

I know I’m a long shot away from completing the challenge but would like to understand the [4, 5] part. Many thanks guys.

``````   **Your code so far**
``````
``````
function largestOfFour(arr) {
let longestNumber = [];
for(let i = 0; i < arr.length; i++){
for (let j = 0; j < arr[i].length; j++){
if (arr[i][j] > longestNumber){
longestNumber.push(arr[i][j])
}
}
}

console.log(longestNumber)

return longestNumber;
}

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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.109 Safari/537.36`

Challenge: Return Largest Numbers in Arrays

My guess would be that once your array longestNumber has two values in it, no individual element in a sub-array (arr[i][j]) is going to be considered bigger than that.

I created a function to test this out myself (where arr_one has one value and arr_two has two:

function question(arr_one, arr_two){

if (arr_one > arr_two){

return true;

} else {

return false;

}}

And that’s how it seems if you compare, say, [1000] to [4,5]. Weirdly if the first value in array_two is 1 or less, it says it is bigger, which makes no sense to me, but I don’t think that will be an issue with this problem.

1 Like

This is close. While the array is a single value, or even no value, the `>` comparison is using the numeric coercion: `Number([]) === 0` and `Number([4]) === 4`.

But when we get to `[4, 5]`, then the Number coercion breaks down: `Number([4,5]) === NaN`. And the `>` comparison silently fails.

@aznoh, I’ll give you the same advice I just gave on another topic: when facing a challenge like this one that might take a little more work, I’ll pull them into a replit (https://replit.com/) and play around with different ideas. see what works and what doesn’t.

I’ll add `console.log()` things in there, get a look at variables inside the loops, and see what’s really going on.

Further, I’d take a little time and think about what is meant by “largest of four” here. What you’re currently doing is, if the value in `longestNumber` is less than the one you’re comparing to, you’re pushing another number onto `longestNumber`. By that approach, you’ll end up with all sixteen numbers in.

Instead, make a plan. When you first get into that outer loop (your `i` loop), you know something about the first value in that nested array. What do you explicitly know about `4` in that first array, or `13` in the second one, `32` in the third, and `1000` in the fourth?

1 Like

Thank you so much. This has really helped me.

1 Like

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