Seek and Destroy: Why doesn't pop() work?

Tell us what’s happening:
I was able to come up with this solution to the “Seek and Destroy” exercise with this recursive function:

function destroyer(arr, ...args) {
  if(args.length < 1)
  {
    return arr;
  }
  else if(args.length == 1)
  {
    return arr.filter(item => item != args[0]);
  }
  else
  {
    return destroyer(arr.filter(item => item != args[0]), ...args.slice(1));
  }
}

My question is this: originally, I was using item != args.pop() instead of item != args[0] and passing args in the final case without slicing. That cleared every element from the array on the second recursion, contradicting my initial assumption that the interpreter was calling pop() twice in my code. Can anyone explain why pop() did this?

  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:94.0) Gecko/20100101 Firefox/94.0

Challenge: Seek and Destroy

Link to the challenge:

.pop() mutates the array.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.