Nodejs Array Multiply multiplyByTwo()

Here you can find the source of multiplyByTwo()

Method Source Code

Array.prototype.multiplyByTwo = function() {
  var newArr = [];

  for (var i = 0; i < this.length; i++) {
    newArr.push(this[i] * 2)//  w ww .  ja v  a  2s.co  m
  }

  return newArr;
};

console.log([1, 2, 3].multiplyByTwo());


Array.prototype.myEach = function(func) {
  for (var i = 0; i < this.length; i++) {
    func(this[i]);
  }
};

[1, 2, 3].myEach( function(x) { console.log(x) })


Array.prototype.myMap = function(func) {
  var newArr = [];

  this.myEach(function(x) {
    newArr.push(func(x));
  });

  return newArr;
}

console.log([1, 2, 3].myMap(function(x) {
  return x * 3;
}));


Array.prototype.myInject = function(func) {
  var acc = this[0];

  this.slice(1, this.length).myEach(function(x) {
    acc = func(acc, x);
  });

  return acc;
};

console.log([1, 2, 3, 4].myInject(function(acc, x) {
  return acc + x;
}));

Related

  1. mult2()
    Array.prototype.mult2 = function() {
        for (var i = 0; i < this.length; i++) {
            this[i] = this[i] * 2;
    };
    
  2. multipleConcat()
    Array.prototype.multipleConcat = function() {
        var initialArray = arguments[0];
        if (arguments.length > 0) {
          for (var i = 1, arrayCount = arguments.length; i < arrayCount; i++ ) {
            initialArray.push.apply(initialArray, arguments[i]);
          return initialArray;
    };
    ...
    
  3. multiples(multiple)
    Array.prototype.multiples = function(multiple) {
      var multiples = [];
      for (var i = 0; i < this.length; i++){
        multiples.push(this[i] * multiple);
      return multiples;
    console.log([1,2,3,4,5].multiples(5));
    Array.prototype.myEach = function(someFunction){
    ...
    
  4. multiply(a2)
    Array.prototype.multiply = function(a2) {
      var l1 = this.length;
      var l2 = a2.length;
      var len = (l1 < l2 ? l1 : l2);
      var result = new Array(len);
      for (var i = 0; i < len; i++) {
        result[i] = this[i] * a2[i];
      return result;
    ...
    
  5. multiply(n)
    Array.prototype.multiply = function(n){
      results = []
      for (var i = 0; i < this.length; i++){
        for (var j = 0; j < n; j++){
          results.push(this[i])
      return results
    
  6. multislice(ranges)
    Array.prototype.multislice = function(ranges) {
      if(typeof ranges === 'undefined')
        throw Exception("Ranges have to be defined!");
      if(typeof ranges !== 'object') 
        throw Exception("You have to pass an array!");
      return ranges.map(el => this.slice(el.from, el.to));
    };
    
  7. multislice(ranges)
    'use strict';
    var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { 
      return typeof obj; 
      } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
    Array.prototype.multislice = function (ranges) {
      var _this = this;
      if (typeof ranges === 'undefined') throw Exception("Ranges have to be defined!");
      if ((typeof ranges === 'undefined' ? 'undefined' : _typeof(ranges)) !== 'object') throw Exception("You have to pass an array!");
      return ranges.map(function (el) {
    ...
    
  8. multisplice()
    Array.prototype.multisplice = function(){
      var args = arguments[0], 
        res = [];
      args.sort( function(a, b){
         return a - b;
       });
      for(var i = 0; i < args.length; i++){
        var index = args[i] - i;
        res.push( this.splice(index, 1)[0] );
    ...
    
  9. mutilate(start, end)
    var arr = ["Devin Hardy", "Seth Rollins", "Cole Sprouse"];
    Array.prototype.mutilate = function(start, end) {
      var newArr = [];
      if (end > this.length || start > this.length) {
        throw new Error("Invalid opperation.");
      } else {
        arr2 = this.slice(start, end);
        newArr.push(arr2);
      return newArr;
    };