Return Largest Numbers in Arrays, failing tests

Why are the test runs failing for my function? I checked in Chrome’s developer console and it runs perfectly with the same results that the test show’s should be returning. Question is to write a function that returns the largest numbers in an array. I attached a screenshot with the failed tests.

Your code so far

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

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 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36.

Link to the challenge:

You are very close to figuring out the problem, double check on what specific error that you are getting, and that will solve the issue.

Hint: It’s in your new_arr variable

I still don’t understand because when I run it in Chrome developer console it is returning an array with the same exact numbers as the test. I need another hint, but don’t give me the answer.

When you run the test through FCC itself, on the bottom right where the results should be, what error message does it give you?

1 Like

OMG!!! lol, thank you. I forgot to look down there. I can’t believe I forgot to define it. Thank you so much for the help.

You’re welcome! You basically had it down, but just forgot to declare that variable. Congrats on getting it!

1 Like

Quick question. I’m checking through Chrome developer console before I actually submit the problem. In Chrome console the solution comes out exactly if I define the variable or not. What can you recommend me to use instead of Chrome developer console to avoid this in future?

The Chrome developer tool is what you would primarily use when building and testing things out, the reason why it’s showing up on the console and undefined in your function is because a variable not being declared will become global, so it shows up in your console. And by using a globally defined variable within a function can result in some weird results.

So best advice is to just always remember to declare your variables, I’m not sure how far into the JS you are in, but should ditch var for let and const, as they each come with it’s own scoping, unlike var, so using those will prevent any headaches in the future.