# Intermediate Algorithm Scripting code not working

I realize how clunky this code is, but I’m not entirely sure why it’s not working. Logging `destroyer([1, 2, 3, 1, 2, 3], 2, 3)` returns `[1, 3, 1]` rather than `[1, 1]` like it’s intended to. Any help would be appreciated.

``````function destroyer(arr) {
let args = [...arguments];
let newArr = [];
for (e = 1; e < (args.length - 1); e++) {
let target = args[e];
for (f = 0; f < (arr.length - 1); f++) {
let arrEl = arr[f]
if (arr[f] !== target) {
newArr.push(arrEl);
}
}
return newArr;
}
}

console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));
``````

Thing 1: You are using `< (arr.length -1)` which will stop without hitting the last item in the array. You usually want to either use `< arr.length` or `<= (arr.length - 1)`.

Thing 2: You are returning `newArr` inside your loop. That means that you only ever check one value.

Thing 3: You are adding to `newArr` as you go, but never removing anything from it. If you fix the problems above, you’ll still get incorrect answers because first you will add all the values that are not `2` (including `3`s) then you will add the values that are not `3` (including `2`) and you will add some values multiple times.

I tried starting over from the beginning here:

``````function destroyer(arr) {
let args = [...arguments];
args.shift();

for (i = 0; i < args.length; i++) {
for (f = 0; f < arr.length; f++) {
var newArr = arr.filter(el => el !== args[i]);
}
}

return newArr;
}

console.log(destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3));
``````

It looks like it’s still only checking one value, even though I’m returning `newArr` outside the loop this time.

You are replacing `newArr` every time the inner loop runs, so at the end of the function only has the last value filtered out.

I managed to solve it using `splice()`. Probably not the most elegant solution, but it works.

Thanks so much for the help. This was super frustrating.

``````function destroyer(arr) {
let newArr = arr;
let args = [...arguments];
args.shift();

for (i = 0; i < args.length; i++) {
for (f = 0; f < newArr.length; f++) {
while (newArr[f] === args[i]) {
newArr.splice(f, 1);
}
}
}

return newArr;
}

console.log(destroyer([1, 2, 3, 1, 2, 3], 2, 3));
``````