Intermediate Algorithm Scripting: Seek and Destroy - Help Needed With Erasing All of Correct Elements

I can’t figure out how to erase all of the elements that match those outside the array in the arguments object. Anyone got any ideas or hints?

Thanks.

Your code so far


function destroyer(arr) {
return arr.filter(elem => {
  for (let i = 0; i < arguments.length; ++i) {
    if (i >= 1 && elem === arguments[i]) {
      arr.splice(arr.indexOf(elem, 1));
    }
  }
  return arr;
})
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36 Edg/83.0.478.64.

Challenge: Seek and Destroy

Link to the challenge:

1 Like

Okay, I noticed a little mistake in my splice call and took out the 1. The destroyer function still returns [1, 2], though. What am I doing wrong?

You should not use splice inside your filter callback. You should return a boolean value from the filter callback, true if the element should be appended to the new array, or false if the element should not. The callback function should not create side effects, it should only deterministically return true or false based on its input.

1 Like

Thanks. That helped.