Symmetric difference

I am struggling to get past the symmetric difference challenge. Here is my code:

function sym(...args) {
  function symDiff(arr1, arr2) {
    var arr1Diff = arr1.filter(x => !arr2.includes(x));
    var arr2Diff = arr2.filter(x => !arr1.includes(x));
    const arr = arr1Diff.concat(arr2Diff);
    return Array.from(new Set(arr.sort()));
  return console.log(args.reduce((a,b) => symDiff(a, b)));

sym([1, 2, 3], [5, 2, 1, 4]);

I think the output matches the intended output, but I am unsure where the code is wrong.

1 Like

console.log has a return value of undefined

1 Like

can you post a link to it

Why this line

   return Array.from(new Set(arr.sort()));

isn’t just

return arr.sort();


Shouldn’t that be unique already?

Thanks for your help! :grinning: Storing the result in a variable and returning the variable solved the challenge.

If I don’t use the sort method, a set is created with unsorted unique values.

I never removed the sort

return arr.sort();

Correction: nope, it doesnt.

Where in the challenge instructions does it say the array needs to be sorted? A set is not necessarily sorted. It is just a collection of unique values. You can get rid of the .sort() and should have no affect on the passing of the challenge.

1 Like

Is there any simple reason why return arr; isn’t working in that step (return of first function)? (Without sorting and making it a set, as it should be a set anyways, I think).

I couldn’t read the whole requirements but it sounds fine to me comparing to OP code.

The OP’s code would not filter out duplicates without creating a Set in the return statement.

For example, calling sym([1, 2, 3], [5, 2, 1, 4, 5]) with only return arr, would result in [3, 5, 4, 5] instead [3, 4, 5].

1 Like

There can not be a duplicate imho in the concatenation, in the way those arrays are filtered.

[1,3,4], [2,4] will return [1,3] and then just [2] and then concatenate.

Also, the code that is logged in the console has all the arrays ordered (not saying this is requirement, but it looks like it.)

Sorry, I got it. The arrays aren’t set from the beginning. That was a wrong assumption. Yes I read the same test from the console.

The tests use assert.sameMembers(), so the order of the array is irrelevant (as it should be).

1 Like