To FCC STAFF: Seek and Destroy challenge solutions

I think that a solution of the Seek and Destroy challenge is false, and shouldn’t be considered as a solution and it is the first solution in the solutions, here is the code for it:

function destroyer(arr) {
  var args = Array.prototype.slice.call(arguments);

  for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < args.length; j++) {
      if (arr[i] === args[j]) {
        delete arr[i];
      }
    }
  }
  return arr.filter(Boolean);
}

I think the flaw in this solution is that the .filter method removes “0” also, because “0” evaluates to false.

I’m not a member of staff but we mod have the permissions to make these edits

can you give the link to the guide?

First solution: freeCodeCamp Challenge Guide: Seek and Destroy

I think return arr.filter(Boolean); can be replaced by this :

return arr.filter((item) => {
    if (Boolean || item===0) {
      return true;
    }
  });

I had a problem in Django in another thread, I think there are some problems.
Can I explain the problem here

it would be off topic, please keep using your own topic for your own question

I’m not sure it would be correct - as it breaks in that case if the arguments start being not numbers

that why is the || or “or” operator, if it is not a number it will check for Boolean, i think the problem was with the “0”.

destroyer([false, null, NaN, 5, 6, 7, "Hi", "Everyone", "", 5, 3, 7], 3, 5, 6, 7)

what’s the output with this call?

You’re correct that the fCC solution is flawed and changing the code to

return arr.filter(item => Boolean || item===0);

fixes it.

But using the .delete method on the array and then removing all undefined values with filter(Boolean) is somewhat, well, not an awesome solution anyway, because it must fail whenever a falsy value is included in the array. It would make more sense to use the filter method within the loop to remove the unwanted item, and then at the end just return the array:

function destroyer(arr) {
  var args = Array.prototype.slice.call(arguments);

  for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < args.length; j++) {
      if (arr[i] === args[j]) {
        arr = arr.filter(e => e !== arr[i])
      }
    }
  }
  return arr
}

And this is still a rather ugly solution compared to the other two. It’s just more beginner-friendly with the nested loops instead of array methods.

also, there is no need to loop over the array with the loop as the filter method is already doing that

Yeah, that too. If a solution with nested for loops should be included, the inner loop should maybe push all valid items into a result array or something.

If you (or someone else, like @bedward that brought this up) write a beginner friendly solution I can add it to the guide, and remove this one that doesn’t work

sharp one ! understood

Technically you can run filter and have it return any truthy value and it will just skip the “holes”. But I’m not sure how acceptable a solution that is.

[0,1,,2,3].filter(() => true)
// [0, 1, 2, 3]

[0,1,,2,3].filter(() => 'test')
// [0, 1, 2, 3]

[0,1,,2,3].filter(() => 42)
// [0, 1, 2, 3]

[0,1,,2,3].filter(Boolean)
// [1, 2, 3]

I think doing that kind of hides what is going on (as I understand it).

22.1.3.7 Array.prototype.filter ( callbackfn [ , thisArg ] )

elements that are deleted after the call to filter begins and before being visited are not visited.