Guest User

Untitled

a guest
Feb 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.76 KB | None | 0 0
  1. ASSIGNMENT
  2. You will be provided with an initial array (the first argument in the destroyer function),
  3. followed by one or more arguments. Remove all elements from the initial array
  4. that are of the same value as these arguments.
  5.  
  6. SOLUTION
  7.  
  8. function destroyer(arr) {
  9.  
  10. var args = Array.from(arguments).slice(1); // create a new array from array-like object (arguments), i.e. (arr) and
  11. // place its values into a new array (var args). Apply .slice method on
  12. // the new array (args), extracting part of it, starting from index 1
  13. // and place it in args again.
  14. // (in our case, [2, 3] will be its values now, as [1,2,3,1,2,3] has index 0.
  15. // (from var args [[1, 2, 3, 1, 2, 3], 2, 3])
  16. return arr.filter(function (elements) {
  17. return !args.includes(elements); // if the element being checked is NOT in the args array (we check it by
  18. }); // putting ! sign in front of args (!args)), return true.
  19. } // arr.filter creates a new array and places there each element
  20. // for which callback function (!args.includes(elements)) returns true
  21. // MDN web docs: filter() calls a provided callback function once for each element in an array,
  22. // and constructs a new array of all the values for which
  23. 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