Seek & Destroy Solution 3 - Error & Suggestion

Seek & Destroy Solution 3 - Error & Suggestion
0

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.

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);
1 Like

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.

1 Like

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 ;(

1 Like