Seek & Destroy, what's wrong with this solution?

Seek & Destroy, what's wrong with this solution?
0.0 0

#1

Hey everyone,

I came up with another solution for the Seek and Destroy algorithm and I’m trying to figure out what some unintended consequences could be using this in the long term. I’m doing some pretty weird stuff setting the gainer equivalent to a filtered version of the gainer within the for loop… is that bad practice?

// jshint esversion:6

function destroyer(arr) {
  // Remove all the values
  var gainer = Array.prototype.slice.call(arguments[0]);

  
  for (var i = 1; i < arguments.length; i++) {
    console.log(arguments[i], gainer);
    gainer = gainer.filter(el => el !== arguments[i]);
  }
  return gainer;
}

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


#2

My solution was virtually the same:

function removeValue(val) {
	 return (val != this);
}

function destroyer(arr) {
  var testArr = arr;
  var args = Array.from(arguments);
  for (var i = 1; i < args.length; i++) {
	testArr = testArr.filter(removeValue, args[i]);
  
  }
  
  return testArr;
}

I’m not sure why you think it is bad practice.


#3

Was the purpose of the following line to make a copy of the first argument? If so, you could have easily written it as below, since it is a one-dimensional array.

var gainer = arr.slice();  

If you did not care about making a copy of the first argument and wanted to work with a variable gainer which represented the first argumnet throughout the code, then you could have just written your function as below:

function destroyer(gainer) {
  for (var i = 1; i < arguments.length; i++) {
    console.log(arguments[i], gainer);
    gainer = gainer.filter(el => el !== arguments[i]);
  }
  return gainer;
}

P.S. When posting a full working solution, surround the code with [spoiler] and [/spoiler] tags, so someone who is still working on a solution, does not see it immediately when they happen to view your topic. The tags need to be on their own separate lines, for the blur effect to work.

Thank you.