Seek & Destroy Solution 3 - Error & Suggestion

Hello,

Looks like Intermediate Algorithm Scripting: Seek and Destroy has a problem with the Suggested Solution #3.

It says

const destroyer = (arr, ...valsToRemove) => arr.filter(elem => !valuesToRemove.includes(elem));

The problem with this is it never defines valuesToRemove so running this code results in an error.

I’d like to propose either of mine instead:

//SUGGESTED CODE 1
function destroyer([...arr], ...destroy) {
  return arr.filter(item => destroy.indexOf(item) === -1);
}

This code is similar to the previous exercise, Diff Two Arrays.

  1. I start by destructuring the argument into arr using the ... spread operator and repeat that process for another destructured component I named destroy.
  2. Next I filter the array arr by the elements in the destroy array and return only those unique values.
//SUGGESTED CODE 2
function destroyer(arr) {
  let args = Array.from(arguments).slice(1);
  return arr.filter(val => !args.includes(val));
}
  1. If arguments is really a necessary component of this exercise (it isn’t tested for), then this uses that solution.
  2. Like Solution 2, it takes the elements you’re looking to destroy (everything after the array) and assigns those values to the new variable, args.
  3. Then just like I did in suggestion 1, filter arr using args and return all unique values.
1 Like

There is indeed an error here, though it’s just a silliy mistake. You just have to change “valuesToRemove” to “valsToRemove” and voila it works.

Here’s what I mean:

const destroyer = (arr, ...valsToRemove) => arr.filter(elem => !valsToRemove.includes(elem));

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

You can write the same code as follows:

function destroyer(arr, ...valsToRemove) {
    return arr.filter(elem => !valsToRemove.includes(elem));
} 

destroyer([1, 2, 3, 1, 2, 3], 2, 3);
2 Likes

Lol good on you for observing that. I definitely missed that parameter misspell when working it out for myself and when providing alternatives.

1 Like

Now who is gonna fix it?

1 Like

I opened a new issue on GitHub:

Nicely done.

I’ve done that a few times and after I was mistaken about the error they instructed me to create a post in the forum. Regardless, we both see a problem with the code so I’m sure it will be taken up.

2 Likes

Yeah, hopefully they’ll fix it. I’ve never posted there before, on GitHub. Let’s see :wink:

1 Like

They fixed it, yay :stuck_out_tongue_winking_eye:
Though it seems like the site hasn’t updated yet ;(

2 Likes

Another solution with some() :

const destroyer = (arr, ...args) => arr.filter((element) => !args.some(e => element === e))

// SUGGESTED CODE

function destroyer(…arr) {
return arr[0].filter((elem) => !arr.includes(elem));
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);

Short an functional way to solve this.

function destroyer(arr, ...args) {
   return arr.filter(e => !args.includes(e));
}

destroyer([1, 2, 3, 1, 2, 3], 2, 3);
1 Like

@alaa-essaies, @anik-io, @tomstor82
You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.

Thanks for your understanding.

I always thought sharing was caring?! It is helpful to see solutions in my opinion, and especially shorter and easier ones. My post implies replies from whomever has suggestions, but I’ll make a note of your comment.

there are solutions in the guide. We want to avoid people looking accidentally at them, to avoid spoilers.

1 Like

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