Symmetric Difference - Tests are wrong

Symmetric Difference - Tests are wrong
0.0 0

#1

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:


#2

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


#3

I can see why you might think that. They give you arrays with duplicate values like 1, 1, …

Symmetric Difference applies to sets. Sets do not have duplicate values. So even though the following has three arrays:

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

in terms of sets, you really have:

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

#4

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


#5

Sets were not introduced to JavaScript until ES6 and the current code camp curriculum focuses on pre-ES6 syntax and features. I believe the yet to be fully released beta version will have more focus on the newer features such as Sets.

The challenge wording could probably be tweaked to remind the user to think in terms of sets.


#6

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])


#7

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.