Seek and Destroy; my own code

So I’ve done the challenge Seek and Destroy in the basic algorithm scripting challenge. It asks me to destroy numbers from an array if that numbers are given to the function.

So what I created:

function destroyForProfit(arrOne, arrTwo){
  for (var i = 0; i < arrOne.length; ++i){
    for (var j = 0; j < arrTwo.length; ++j){
    if (arrOne[i] === arrTwo[j]){
  return arrOne;

function destroyer(arr) {
  // Remove all the values
  var clayArray = [];
  var arrOne = [];
  var arrTwo = [];  
  for(var i = 0; i < arguments.length; ++i){
     clayArray[i] = new Array(1);
     clayArray[i] = arguments[i];
    arrOne = clayArray[0];
    arrTwo[i] = clayArray[i];
  arrOne = destroyForProfit(arrOne, arrTwo);
  arrOne = destroyForProfit(arrOne, arrTwo);
  return arrOne; 

I’m happy to say that it works. The question I have is that I dont know why I have to go through destroyForProfits TWICE (or more)? If I go through the function once, it doesn’t work. Can somebody explain to me why and what I did wrong?

This is the text provided by the challenge (so you guys may not have to look it up):

You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments. Remove all elements from the initial array that are of the same value as these arguments.

destroyer([1, 2, 3, 1, 2, 3], 2, 3) should return [1, 1].
destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3) should return [1, 5, 1].
destroyer([3, 5, 1, 2, 2], 2, 3, 5) should return [1].
destroyer([2, 3, 2, 3], 2, 3) should return [].
destroyer(["tree", "hamburger", 53], "tree", 53) should return ["hamburger"].

Hello, to post your code on the forums you need to put in between triple back ticks.

I’ve run your code and called destroyForProfit only once and it worked.

That being said you did a lot of work here.

Did you take a look at the link they provide in the challenge about arguments ?

Also, what do you need that line for ?

I never find it a good idea to modify an array I’m looping over.

Also, I believe your doing an awful lot of work to get the arguments, but that is not the point here.

This line:


changes the original array, so once you remove duplicate the index i gets out of sync with positions in arrOne, and if items you have to remove follow one by one you remove first one and skip second. That’s why you need two passes with your code.

I think I know what you guys mean. I didn’t need the clayArr. But what I’m sometimes afraid for is when I do stuff to my variables, it sometimes doesn’t recognise the var as being an array, so I get an error. But the arrOne that is passed to the other function could as well have been arguments[0]. clayArr is an obsolete variable. The thing is I didn’t want to change the arguments, so I declared a new variable to take it over. I failed at that mission as well.
@jenovs thanks for this explanation, I didn’t think about that. Do you have any idea how I could run through the function only once, without changing much?

Dude, you solved the challenge. You can refactor later, when you’re more comfortable. Take the reward :wink:

Without changing too much of you code:

function destroyForProfit(arrOne, arrTwo) {
  var newArr = [];
  for (var i = 0; i < arrOne.length; ++i) {
    if (arrTwo.indexOf(arrOne[i]) === -1) {
  return newArr;

Take each element of arrOne and check if arrTwo hasn’t it (=== -1); add to newArr; return newArr.

@jenovs it works! And I think I understand how it works too. Thanks a lot for this!