Advertisement
dimipan80

Array Prototype Function

Nov 19th, 2014
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Write a JavaScript function removeItem(value) that accept as parameter a number or string.
  2. The function should remove all elements with the given value from an array. Attach the function to
  3. the Array type. You may need to read about prototypes in JavaScript and how to attach methods to object types.
  4. You should return as a result the modified array. */
  5.  
  6. "use strict";
  7.  
  8. Array.prototype.removeItem = function(value) {
  9.     return this.filter(filterElementsByValue);
  10.  
  11.     function filterElementsByValue(element) {
  12.         return element !== value;
  13.     }
  14. };
  15.  
  16. var arr = [1, 2, 1, 4, 1, 3, 4, 1, 111, 3, 2, 1, '1'];
  17. console.log(arr.removeItem(1));
  18.  
  19. var arr = ['hi', 'bye', 'hello' ];
  20. console.log(arr.removeItem('bye'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement