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 ![]()
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:
https://www.freecodecamp.org/challenges/symmetric-difference