Javascript Array duplicator()

Description

Javascript Array duplicator()


Array.prototype.duplicator = function() {
  var len = this.length;
  for (var i = 0; i < len; i++) {
    this.push(this[i]);//  w w w . j  a v a  2  s  .  c  o m
  }
  return this;
};

console.log([1, 2, 3, 4, 5].duplicator());

Javascript Array duplicator()

const arr = [ 1, 2, 3, 4, 5 ];// w w  w. ja v  a 2 s .  com

Array.prototype.duplicator = function () {
    const newArr = this.concat(this);
    return newArr;
};

function run()
{
    console.log(arr.duplicator());
}

export default {
    run
}

Javascript Array duplicator()

// Make this statement work.
// [1,2,3,4,5].duplicator();
// result: [1,2,3,4,5,1,2,3,4,5]

Array.prototype.duplicator = function() {
  return this.concat(this);
}

var arr = [1,2,3,4,5];
console.log(arr.duplicator());//from ww  w . ja v  a2  s .  c  om

Javascript Array duplicator()

Array.prototype.duplicator = function() {
    if (this.length <= 0) {
        return [];
    }//ww w  . j a v a  2 s  . c  o m

    return this.concat(this);
};

var c = [].duplicator();
console.log(c);

function add(n) {
    var fn = function(m) {
        return add(n + m);
    }

    fn.valueOf = function() {
        return n;
    }

    fn.toString = function() {
        return '' + n;
    }

    return fn;
}

console.log(add(1)(3)(4)(5));



PreviousNext

Related