Javascript Array removeValue(thing)

Description

Javascript Array removeValue(thing)


/**/*from   w w  w  .  j av  a2  s.c  o m*/
 * The object of this is to create a function called removeValue that removes
 * all of a certain value from an array using .splice() and then returns the array.
 * If the array is empty or has no element to remove it should return false.
 * 
 * example:
 * arr = [1, 2, 1, '1', 3]
 * arr.removeValue(1); // return [2, '1', 3]
 */

Array.prototype.removeValue = function(thing) {
 for (var i = 0; i < this.length; i++) {
  if(this[i] == thing) {
   this.splice(i, 1);
  }
 }

 return this;
}

Javascript Array removeValue(thing)

/**/*from   w  ww  .j  a  v a 2  s  .  co  m*/
 * 7 kyu: Splicing
 *
 * The object of this kata is to create a function called removeValue that removes all of a certain
 * value from an array using .splice() and then returns the array. If the array is empty or has no 
 * element to remove it should return false.
 * 
 * example:
 *
 * arr = [1, 2, 1, '1', 3]
 * arr.removeValue(1); // returns [2, '1', 3]
 */

Array.prototype.removeValue = function(thing) {
 if (this.indexOf(thing) === -1) return false;
 var i;
 while ((i = this.indexOf(thing)) > -1) this.splice(i, 1);
 return this;
}



PreviousNext

Related