(SPOILERS)Seek And Destroy help(soved but want a better solution)

(SPOILERS)Seek And Destroy help(soved but want a better solution)
0

#1

Here is code, I put everything in a spoiler tag. please only look if you have solved this one too.


var tmpHolder=0;
function scn(arg){

return (arg!=tmpHolder);
}
function destroyer(arr) {
  // Remove all the values0
  var newArray=arguments[0];
  var numArgs=arguments.length;
  var scanArray=[];
  for(var i = 1; i< numArgs;i++){scanArray.push(arguments[i]);}
  for(var j=0;j<scanArray.length;j++){
    tmpHolder=scanArray[j];
    newArray=newArray.filter(scn);
  }
  return newArray;
}

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


Now How can I do this differently? I hate having this ugly single use Global variable “tmpHolder”. is there a way to pass in a second argument into the filter function?

Arr.filter(function("step in array", "second argument");

#2

You don’t need that global thing at all. You can do e.g.

// The function takes a single named argument,
// this is the array you want to filter:
function destroyer(arr) {
  // Get the rest of the arguments, the targets you
  // want to destroy. This will be in the form of
  // an array, so for `destroyer([1,2,3], 1, 2)`
  // targets will be [1, 2]
  var targets = Array.from(arguments).slice(1);
  // Then filter the original array. For each value in it,
  // check if it is NOT one of the values in `targets`
  return arr.filter(function(v) {
    return targets.includes(v) === false;
  });
}

Key thing is that for you want to filter the initial array (the first argument), and in the callback return false for any value that exists in an array of the remaining arguments. This will filter those values out, leaving you with a new array containing the non-destroyed values.

For something more compact again, that has no intermediate variables, you can adjust the function signature:

function destroyer(arr, ...targets) {
  return arr.filter(v => !targets.includes(v));
}