Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ASSIGNMENT
- 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.
- SOLUTION
- function destroyer(arr) {
- var args = Array.from(arguments).slice(1); // create a new array from array-like object (arguments), i.e. (arr) and
- // place its values into a new array (var args). Apply .slice method on
- // the new array (args), extracting part of it, starting from index 1
- // and place it in args again.
- // (in our case, [2, 3] will be its values now, as [1,2,3,1,2,3] has index 0.
- // (from var args [[1, 2, 3, 1, 2, 3], 2, 3])
- return arr.filter(function (elements) {
- return !args.includes(elements); // if the element being checked is NOT in the args array (we check it by
- }); // putting ! sign in front of args (!args)), return true.
- } // arr.filter creates a new array and places there each element
- // for which callback function (!args.includes(elements)) returns true
- // MDN web docs: filter() calls a provided callback function once for each element in an array,
- // and constructs a new array of all the values for which
- destroyer([1, 2, 3, 1, 2, 3], 2, 3); // callback returns a value that coerces to true.
Add Comment
Please, Sign In to add comment