Build a Symmetric Difference Function - Build a Symmetric Difference Function

Tell us what’s happening:

The tests on this one only checks against those that are different in the first array and therefore can be passed without returning unique values in the 2nd array.

I would recommend including a further test to check against the example given in the description text

Array A: [“diamond”, “stick”, “apple”]
Array B: [“stick”, “emerald”, “bread”]
Result: [“diamond”, “apple”, “emerald”, “bread”]

Your code so far

/*
Compare two arrays and return a new array with any items only found in one of the two given arrays, but not both. In other words, return the symmetric difference of the two arrays.

Example:
Array A: ["diamond", "stick", "apple"]
Array B: ["stick", "emerald", "bread"]
Result: ["diamond", "apple", "emerald", "bread"]
*/

function diffArray(arr1,arr2) {
  const differentElements = arr1.filter((str) => arr2.indexOf(str) === -1);
  console.log(differentElements);
  return differentElements;
}
console.log(diffArray(["diorite", "andesite", "grass", "dirt", "pink wool", "dead shrub"], ["diorite", "andesite", "grass", "dirt", "dead shrub"])) //should return ["pink wool"].
console.log(diffArray(["diamond", "stick", "apple"],["stick", "emerald", "bread"]))

Your browser information:

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

Challenge Information:

Build a Symmetric Difference Function - Build a Symmetric Difference Function
https://www.freecodecamp.org/learn/full-stack-developer/lab-symmetric-difference/lab-symmetric-difference

Thank you for helping make FCC better. Bugs can be reported as GitHub Issues. Whenever reporting a bug, please check first that there isn’t already an issue for it and provide as much detail as possible.

1 Like

that was better than my code, But if the unique value is in Array B will not be in the resulted Array.
I think we have to repeat the check for the second Array (arr2.filter …)

the tests have been updated, now this code would fail

1 Like