Javascript Array remove(index)

Description

Javascript Array remove(index)


Array.prototype.remove = function(index)
{
    this.splice(index,1);/*  www  .j av  a 2s . com*/
};

Javascript Array remove(index)

// Create an array remove function, that removes an element from given array

'use strict'//from   ww  w . j ava2s  .c  om

Array.prototype.remove = function remove(index) {
  if(typeof index !== 'number' || index < 0 || index > this.length) return;

  this.splice(index, 1);
}

let myArray = [1, 2, 3];
myArray.remove(1);
console.log(myArray);

/*
  Comments: I failed at this one. This was my first time extending a class.
  I learned about adding methods to an existing class by manipulating their
  prototype key.
*/

Javascript Array remove(index)

Array.prototype.remove = function(index) {
  this.splice(index,1);/*from  www.  ja  v a 2 s  . com*/
}

function solution(array) {

//console.log(array);

  var originalArray = array;
  var N = array.length;
  var sumArray = [];
  var sum;
  //console.log(array);

  for(var i=0; i < N; i++) {

    array.remove(i);
    //console.log(array);
    for(var j=0; j<array.length; j++) {
      console.log(array[j]);
    }


    array = originalArray;
  }

return;

}

solution([1,2,3,4,5]);
//console.log(answer);



PreviousNext

Related