I am having issues trying to get the tests passed for a JS exercise. Please Help!

Tell us what’s happening:
Hi, I am trying to figure out this JS exercise, (see the link that’s at the bottom of this threat)
I have done the exact same thing in VS Code and I do get all the expected results, but when I do it from here, the freeCodeCamp, all of the tests fail :confused:
Has any one else had this same problem? Any help will be greatly appreciated.


Below is my solution:

function uniteUnique(...arr) {
const newArr = [];
arr.flat().map((num) => newArr.indexOf(num) === -1 && newArr.push(num));
return newArr;
uniteUnique([1, 3, 2], [5, 2, 1, 4], [2, 1]);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36

Challenge: Sorted Union

Link to the challenge:

Don’t use a map like a loop. A map makes an array and that array should be used.

You seem to only be using the first array, not all arguments. You’re missing most of the inputs.

Thanks, I appreciate the tip.
The problem is that the solution I have posted there works, if you copy that code snippet and paste it into VS Code you will see that it works perfectly fine, the function returns the array of numbers that are expected in the exercise to get the tests passed.
Thanks anyway

It should not work. You are skipping all but the first argument.

Oh, also, I am not just using the first array, Array.prototype.flat() combines the arrays together into one.

I am telling you, it works!

I’m telling you that you are wrong. It isn’t passing the tests because it does not work.

Flat takes a single array and flattens it. Flat has no way to grab extra arrays that not inside of the original array that the method is called upon.

function uniteUnique(arr) {

I think you are wrong, Copy and paste my code in VS Code, it will work!

Tell me what this does then!
[[1, 3, 2, 3], [5, 2, 1, 4], [2, 1]].flat()

this combines the arrays inside the array into one

Ah, you edited this now. arr is an array of all inputs instead of just the first argument… Big difference. The original code you posted just doesn’t work the way you claim, but the edit is a very different code.

This still should be replaced though.

yeah that is my actual code, but the very strange thing here is that it does work in Visual Studio Code, that is my point

No. Without the rest syntax this doesn’t work. With the rest syntax it does. The two pieces of code have different behavior.

Yeah I get that, but with the rest syntax it does work but only in visual studio code, inside the free code camp ide none of the tests pass

None of the tests pass without the rest parameter, yes.

With the rest parameter this passes (but should be rewritten to not [mis]use map).

I literally just copy-pasted your code and used the function signature with the rest parameter and passed all tests.

1 Like

Yeah like I said, thank you for that tip, it is actually much appreciated.

It’s strange the tests are not passing for me, I might be doing something wrong, I have Ad-Block disabled, anyways. Thanks for your time!

I don’t know what to tell you. Your edited code technically passes.

1 Like