Code Review: Seek and Destroy (Incorrect use of foreach?)

Code Review: Seek and Destroy (Incorrect use of foreach?)
0

#1

What am I doing wrong here? The code returns [1,2,1,2] even though I have kept the condition to be false if the element in the initial array is equal to the value of the other arguments.

I haven’t looked at any hints because I want to try and figure out what I’m doing wrong rather than look for alternative ways of solving this.

Is my use of foreach incorrect? I intend it to loop through each element in the initial array and compare it with each value from the arguments.

function destroyer (arr) {

  var initialArr = Array.prototype.shift.apply(arguments);
  var args = Array.prototype.slice.call(arguments);
 
  args.forEach(function eachArg(value) { 
  arr = initialArr.filter(function check(element) { 
       return (element !== value);
       });  
  });
  return arr;
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);


#3

I had no idea that mattered. I thought whether I set initialArr or arr equal to the filter function it shouldn’t make a difference since it was just returning the result to an array.