Symmetric Difference - Tests are wrong

I am reporting an error with the tests of this exercise. The following tests are wrong:

sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]) should contain only three elements.

Wrong. The Symmetric difference of that is [1,1,4,5,5]

sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]) should contain only five elements.

Wrong. The Symmetric difference of that is [7,4,6,6,2,3]

sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]) should contain only eight elements.

Wrong. The Symmetric difference of that is [7,4,6,6,2,5,9,8,1]

I verified these results with this Calculator, in the case that my code was to blame. But the results were the same. I think the problem is that the test results don’t take repeated numbers into account.

Please tell me if I’m wrong about this. Thanks for reading :slight_smile:

My Code (Don’t look if you didn’t do this exercise yet, hehe)

/*jshint esversion: 6 */

function diffArray(arr1, arr2) {
  return getUnion(getIntersection(arr1, arr2, true, false),getIntersection(arr1,arr2, false, true));  
}

function getUnion(arr1,arr2){
  return arr1.concat(arr2);
}

function getIntersection(arr1, arr2, negate1 = false, negate2 = false){
  var result = [];
  
  if(negate1&&negate2)
    return [];
  
  var loopedArr = negate1 ? arr2 : arr1;
  var otherArr = negate1 ? arr1: arr2;
  
  for(var i = 0;i<loopedArr.length;i++){
    if(!myXOR(otherArr.includes(loopedArr[i]),!negate1 && !negate2))
      result.push(loopedArr[i]);
  }
  
// long way to do it
  
  
//   if(negate1){
//     for(var i = 0;i<arr2.length;i++){
//       if(!arr1.includes(arr2[i]))
//         result.push(arr2[i]);
//     }
//   }
//   else if(negate2){
//     for(var i = 0;i<arr1.length;i++){
//       if(!arr2.includes(arr1[i]))
//         result.push(arr1[i]);
//     }
//   }
//   else{
//     for(var i = 0;i<arr1.length;i++){
//       if(arr2.includes(arr1[i]))
//         result.push(arr1[i]);
//     }
//   }
  return result;
}

function myXOR(a,b) {
  return ( a || b ) && !( a && b );
}

function sym() {
  
  var args = [...arguments];
  
  if(args.length > 2)
    return sym(diffArray(args[0], args[1]), ...args.slice(2));
  
  return diffArray(args[0], args[1]);
}

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

My browser information:

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

Link to the challenge:

this problem is a little confusing as if more than two arrays … it involves getting symmetric of first two arrays and make one array of them eg from above [1, 1, 2, 5], [2, 2, 3, 5] would be [1,3] … no duplicates either …
then symmetric diff of [1,3] and [3,4,5,5] … [1,4,5] so result is three elements.

took me a few reads of the problem before i fully understood what they wanted as it didnt seem to match what was being said on the site explaining what symmetric difference ment.
if you had 4 arrays then you would use [1,4,5] and get the symmetric difference against the next array … etc etc

Ah, I see. Thank you. But in that case, shouldn’t the tests provide actual Sets, then?

No, I meant that the tests should use arrays that actually represented Sets. No need of any extra features.

For example, instead of sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])

…it should be sym([1, 2, 5], [2, 3, 5], [3, 4, 5])

Having to deal with duplication is part of the challenge of this task.

When it comes to the wording of the challenge, if you have suggestions on how to make it clearer, feel free to open a GitHub Issue.

2 Likes