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.
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.
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
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.