Removing all matching values from array

Removing all matching values from array
0
#1

Hi,

I’m working on problem ’ Intermediate Algorithm Scripting: Seek and Destroy’. I can’t post links yet: learn . freecodecamp . org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/seek-and-destroy

Is there an easy way for the argument values to be all removed from the array?

My code:

function destroyer(arr, b, c) {
  for (var i = 1; i < arguments.length; i++) {
    if (arr.indexOf(arguments[i]) > -1) {
      arr.splice(arr.indexOf(arguments[i]), 1);
    }
  }
  console.log(arr);
  //return arr;
}

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

Gives me [1, 1, 2, 3], but I want [1, 1].

#2

.filter() is good start

#4

That is kind of the whole point of the challenge. You have already learned how to capture certain arguments passed to a function using the “rest syntax”, so think how you could use it to your advantage here. Also, consider how you could use this with the filter method recommended by @kerafyrm02.

1 Like
#5

Thanks! Will give it another shot using filter().

#6

Solved it. Thanks a lot for the help.

function destroyer(arr) {
  for (var i = 1; i < arguments.length; i++) {
    var arr = arr.filter(numbers => numbers != arguments[i]);
  }
 return arr;
}

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