Symmetric Difference; 3 questions

3 questions :slight_smile:

  1. For set sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) should return [1, 4, 5].; Why is 5 in it? I do understand that 1 and 4 are rather unique in their sets

  2. Is there a way to run through all your code and see exactly what is happening per phase with example values? I could do console.log everywhere but I remember a tool that really showed what was happening? Because;

  3. I have the idea that the result of “uniqueVals” in the first example is [3,5,4]. Therefore, I don’t understand that reducing the “total” argsArray with something that in my eyes is the final result does give the final right result (in my eyes it should give the opposite or in other words; reducing the full array with the wrong values should give the right result). Apparently I don’t understand reducing good enough…

Feeling me :slight_smile: ?
Please help!!

Your code so far

function sym(args) {
  var argsArray = [].slice.call(arguments);
  function symDiff(arr1,arr2){
    var uniqueVals = [];
    for(var i = 0; i<arr1.length; i++){
      if(arr2.indexOf(arr1[i])<0 && uniqueVals.indexOf(arr1[i])<0){
        uniqueVals.push(arr1[i]);
      }
        
    }
    
    arr2.forEach(function(item){
      if(arr1.indexOf(item)<0 && uniqueVals.indexOf(item)<0){
        uniqueVals.push(item);
      }
    });
    return uniqueVals;
  }
  return argsArray.reduce(symDiff);
}

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

Your browser information:

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

Link to the challenge:
https://www.freecodecamp.org/challenges/symmetric-difference

All right thanks and sorry for posting the solution :slight_smile:

Now I understand what is needed… But I wonder how the arrays are running through the code…

Here’s my try;

  • Create one array of all arguments
  • Reduce this array by running a function SymDiff;
  • A function that takes a first and a second argument and puts the unique numbers in uniqueVals
  • ? The function then picks uniqueVals and the third argument available?
  • ? The result is [1,4,5] and it reduces the full array with that but still has [1,4,5] as a result?

Weird… So simple actually :smiley: Thank you so much!!!