Return Largest Numbers in Arrays won't pass

Hi guys, not sure why this code isn’t passing - it doesn’t pop up any message or error and it handles all the test cases in console. When I click submit answer nothing really happens. Have tried refreshing the browser and looking on the forum to see if there are issues with the question and had a look over my code but I can’t figure it out. Thanks for the help.

function largestOfFour(arr) {
let largestNumbers = [arr[0][0], arr[1][0], arr[2][0], arr[3][0]];
for (let arrIndex = 0; arrIndex < arr.length; arrIndex++)
for(let subArrIndex = 0; subArrIndex < arr[arrIndex].length; subArrIndex++)
if (arr[arrIndex][subArrIndex] > largestNumbers[arrIndex]){
largestNumbers[arrIndex] = arr[arrIndex][subArrIndex];
}
return largestNumbers;
}


**Your browser information:**

User Agent is: <code>Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36</code>.

**Link to the challenge:**
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/return-largest-numbers-in-arrays/

Maybe try a diff browser? Chrome?

I am using Chrome at the moment but I’ll try a different browser when I get home, currently at work!

So I tried to do the test using map() and reduce() and that seemed to pass fine - is there an issue with the question when using for loops possibly? Is there a relevant area I can post issues/bugs regarding questions?

Can you add a working link to the challenge so we can refer back to it? The one above seems to be broken.

largest number arrays challenge
That one should work

i just tried your original code in the challenge and it doesn’t even run for me. I’m going to see if there is a syntax error in it or something causing that effect… I will edit my post again soon…

Edit: I couldn’t find a syntax error in your original code but when I added the for loop braces that are traditionally supposed to be there (but don’t need to be there in your case), the run command worked (but the code didn’t pass the tests). It does seem like there might be an issue with FCC because the test didn’t handle the original version of the code.

function largestOfFour(arr) {
  let largestNumbers = [arr[0][0], arr[1][0], arr[2][0], arr[3][0]];
  for (let arrIndex = 0; arrIndex < arr.length; arrIndex++) {
    for (let subArrIndex = 0; subArrIndex < arr[arrIndex]; subArrIndex++) {
      if (arr[arrIndex][subArrIndex] > largestNumbers[arrIndex]) {
        largestNumbers[arrIndex] = arr[arrIndex][subArrIndex];
      }
    }
  }
  return largestNumbers;
}
largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]);

Yeah I just tried your updated code and it ran for me. It’s odd that it doesn’t work when the tests manage to run, you’re probably right that there’s an issue with FCC. If you’d like any more information just let me know.

i searched github for a similar issue that was reported for this challenge but none of the ones i found were quite the same as yours.
Therefore I do suggest opening a new issue for this:

Hi,

Just had a play with your code and got it to pass, so the challenge is working fine (you need to add the brackets in the correct place). There are errors in the console with your code- it looks like the code is being run through babel.

The code that @hbar1st posted has an error in the inner for loop- it is missing the length property.

You can use console.log(largestOfFour… etc) to test that you are getting the correct output.

Hope this helps and sorry for jumping in on your thread :slight_smile:

PS: I tried the code on repl.it and it ran fine, hence why I think it might be to do with the transform (babel).

I think you missed my point. The code was syntactically correct so there is no reason for the ‘run’ button to fail.
That’s why I suggested he open an issue. (I don’t care that the logic was wrong, just that the syntax was ok to begin with without the braces).

Okay so it is working with the curly braces - I think the only issue here is like @hbar1st said the run button didn’t respond without the curly braces even though it is technically correct (even if the braces should be there for readability).

Sorry, I did miss your point :frowning_face:. (I am dyslexic so I occasionally read everything in the wrong order).

Stylistically, it is better to use braces all the time and many linters will throw errors otherwise, so I would say that the challenge is really enforcing good habits.

I know you have much experience as a software engineer, so would be interested if you have cases where it is better to use no curly braces?

The only time I would condone writing for loops without braces is if they are immediately nested within each other. (and that is common practice in software dev). I would personally have reviewed this code and suggested adding braces to the inner loop even though it doesn’t require it. (not for readability but to prevent future errors when people are maintaining the code)

I would never condone a software interface that leaves the user confused and stuck though. So if the Run command gave some kind of error or if there were known usage rules about usage of braces on FCC (which as far as I know there isn’t), then I would be ok with the behaviour of not accepting a valid syntax. (but no error or warning, isn’t good UXD).

Hope this clarifies things.

1 Like

Thanks, it does help.

I think that FCC’s error handling could be better in all areas, particularly for beginners :slight_smile:

1 Like