[Solved] Return Largest Numbers in Arrays Solved but Doesn't Pass

Hi all,

This is my first post. I’ve met the requirements to pass the challenge below, but for some reason, it won’t recognize it as a legitimate answer. I’m hoping someone could point out something simple I’ve overlooked or confirm that this is some kind of glitch.

var finalArray=[];
function largestOfFour(arr) {
  for (var i = 0; i < arr.length; i ++) {
   finalArray.push(Math.max.apply(null, arr[i]));   
} 
return finalArray;
}

When running your code on the 2nd test case I get: [5, 27, 39, 1001, 27, 5, 39, 1001].

When running on the 3rd test case I get: [5, 27, 39, 1001, 27, 5, 39, 1001, 9, 35, 97, 1000000].

1 Like

Thanks for checking it out.

When i run it in the console, i always get a bunch of added numbers like that.

Was this from the console or the FCC return screen? In the console I get the numbers you say, but in the return screen, I get what I’m supposed to. I’ve passed previous challenges this way.

It was in the console. Not to sure why it’s worked in the past, maybe its caught up with you :grin:?

don’t declare the global variable in fcc challenge
cause fcc run 3 test at first test your global variable finalArray=[] is empty but after first test it is no longer empty
[]
[5, 27, 39, 1001]
[5, 27, 39, 1001, 5, 27, 39, 1001]
[5, 27, 39, 1001, 5, 27, 39, 1001, 27, 5, 39, 1001]

To avoid this, just call that global variable finalArray=[] inside your
largestOfFour function

1 Like

Haha. Maybe so.

But I’ve read the FCC site runs other stuff in the background that interferes. Others have also said they notice their test running 2-3 times on udemy too.

This goes to show in a way then it’s good practice to declare temp variables inside a function.
May I ask which browser you are using to complete the exercises?

Anyway, thanks for feedback!

Awesome. Cut and Paste!

And thanks for confirming the 3-run test.

I initially moved it out cause I couldn’t push to it when using other methods.
Instead of pushing I was hoping I could just return Math.max.apply(null, arr[i]) as an array, but it would just return null.

It was a quick fix. Here’s my solution. Time to check out other solutions and compare! Thanks @mones-cse

function largestOfFour(arr) {
  var finalArray=[];
  for (var i = 0; i < arr.length; i ++) {
   finalArray.push(Math.max.apply(null, arr[i]));   
} 
return finalArray;
}