Intermediate Algorithm Scripting - Seek and Destroy

Tell us what’s happening:
Please i’m very confused on why the code below is giving a different result, ive been trying to crack through the logic on why it is not giving the desired result yet i can’t figure it out. Guess my head is currently blocked lol! Please can someone tell me what wrong with this code, why its outputting [1,2] instead of [1]. thanks

Your code so far

function destroyer(arr) {
  for(let i = 1; i < arguments.length; i++){
    for(let j = 0; j < arr.length; j++){
      if(arguments[i] === arr[j]){
        var seekIndex = j;
        arr.splice(seekIndex,1);
      }
    }
  }
  return arr;
}

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

Your browser information:

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

Challenge: Intermediate Algorithm Scripting - Seek and Destroy

Link to the challenge:

I would not use splice here - this will mutate the array and cause problems with indexing.

Have been trying to wrap my head around the logic, thanks a lot, would rather create a new array and push the values. Thanks

1 Like

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