Intermediate Algorithm Scripting - Seek and Destroy

I can’t imagine that my code is far off, but my results show no success in terms of the test check marks. I am hoping that I am off by a little bit. Can anyone see if I am close?

  **Your code so far**
function destroyer(arr) {
let length=arr.length;
for (let i=1; i<length; i++) {
  let length0=arr[0].length;
  let clips=0;
  if (length0>0) {
    for (let j=0; j<length0; j++) {
      if (arr[0][j-clips]==arr[i]) {
        arr[0].splice(j-clips, 1);
        clips++;
      }
    }
  }
}
return arr[0];
}

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/104.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Seek and Destroy

Link to the challenge:

You are removing items as you iterate through the array. That creates difficult to diagnose bugs. I recommend you not do that. I would instead make a brand new output array.

You might want to console.log(arr[0][j-clips]) before this if statement. You might be surprised what it is. In general, I think you are misunderstanding what arr[0] is.

So it seems that arr[0] is not the destroyer function array.

Nope, the first argument passed to the function is represented by arr and it is the array that gets passed to the function. arr[0] is the first element of the array that gets passed to the function.

Your current code does not access any of the other arguments passed to the function.

Look at the Note for this challenge. You will have to do some research on what this is and how to use it.

1 Like

There is a note in the task:

Note: You have to use the arguments object.

I suggest you to use this logging somewhere in the function:

console.log(arguments)

That should give you some ideas about approach or further research.

In my solution I used combination of methods: filter(), includes() and slice().
But it’s not a big deal - if you’ll understand structure of arguments object - you will be able to implement bunch of different solutions for this task.

Simple example of referencing the arguments object. Let’s say you wanted to accept multiple arguments to a function and then return an array that includes only arguments with an even index (i.e. 0, 2, 4, 6, 8). You could accomplish that with the following function.

function getEvenArgs() {
  const evenArgs = [];
  for (let index = 0; index < arguments.length; index++) {
    if (index % 2 === 0) {
      evenArgs.push(arguments[index]);
    }
  }
  return evenArgs;
}

console.log(getEvenArgs('tree', 'dog', 'cat', 'person')); // [ 'tree', 'cat' ]

NOTE: You will notice there are no parameters in the function declaration.

So the first battle is to properly import the destroyer function and all the arguments. In some cases it a an array of numbers with an unknown number (2 or 3) of number arguments. However, in some cases the destroyer function array is a mix of strings and numbers and the unknown number of arguments is also strings and numbers. I am going to try some things. Maybe the spread thingy will work here.

all I had to do was change the first line arr to …arr and it worked.

Now that I look at some of the elegant solutions, I guess I hooked and crooked a pretty clunky method.

1 Like

That is the story of learning to code - I have said that very same thought to myself so many times