Symmetric Difference problems

Symmetric Difference problems
0

#1

Hey everyone! I’m having a really hard time with the symmetric difference algorithm and I can’t quite figure out why my code isn’t working for one case in particular

function sym(args) {
  var argsArray = [...arguments];
  
  function symDiff(arr1,arr2){
    var holderArray = [];
    arr1.forEach(function(item){
      if (arr2.indexOf(item)<0 && holderArray.indexOf(item)<0){
        holderArray.push(item);
      }
    });
    
    arr2.forEach(function(item){
      if(arr1.indexOf(item)<0 && holderArray.indexOf(item<0)){
        holderArray.push(item);
      }
    });
    return holderArray;
  }
  return argsArray.reduce(symDiff);
}

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

This code works with all of the test cases except the one I have in there now ([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]). The result for that case is coming up [1, 4, 5, 5] and I can’t seem to figure out how that last 5 is sneaking in there. Any ideas? Any help is very much appreciated!


#2

Nevermind, found the problem. In my second forEach function, I had a parenthesis in the wrong place.

if (arr1.indexOf(item)<0 && holderArray.indexOf(item<0))

instead of:

if (arr1.indexOf(item)<0 && holderArray.indexOf(item)<0)