Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SabrinaMarkon/187345d726956afff4199ed110cc2eb5 to your computer and use it in GitHub Desktop.
Save SabrinaMarkon/187345d726956afff4199ed110cc2eb5 to your computer and use it in GitHub Desktop.
Sabrina Markon - Algorithms - Array with argument 0 being a sub array. We want to use the other unknown number of elements as arguments to remove their values from the sub array.
Seek and Destroy
----------------
You will be provided with an initial array (the first argument in the destroyer function), followed by one or more arguments.
Remove all elements from the initial array that are of the same value as these arguments.
function destroyer(arr) {
// Remove all the values
// arr array is the first element in the destroyer function, while the other arguments
// are in the arguments array:
var removeUs = [];
// put all arguments we want to remove from arr into their own array:
for (var i = 1; i < arguments.length; i++) {
removeUs.push(arguments[i]);
}
// function for filter method to remove elements from arr.
function getRidOf(arr){
return removeUs.indexOf(arr) === -1;
}
return arr.filter(getRidOf);
}
destroyer([1, 2, 3, 1, 2, 3], 2, 3);
TESTS:
destroyer([1, 2, 3, 1, 2, 3], 2, 3) should return [1, 1].
destroyer([1, 2, 3, 5, 1, 2, 3], 2, 3) should return [1, 5, 1].
destroyer([3, 5, 1, 2, 2], 2, 3, 5) should return [1].
destroyer([2, 3, 2, 3], 2, 3) should return [].
destroyer(["tree", "hamburger", 53], "tree", 53) should return ["hamburger"].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment