Nodejs Array Multiply multiples(multiple)

Here you can find the source of multiples(multiple)

Method Source Code

Array.prototype.multiples = function(multiple) {
  var multiples = [];
  for (var i = 0; i < this.length; i++){
    multiples.push(this[i] * multiple);/*  ww w . j  a va 2  s .co m*/
  }
  return multiples;
}

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

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

var putsFunction = function(value){
  console.log(value);
}

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

Array.prototype.myMap = function(someFunction){
  var secondArray = [];
  function pushChangedValue(value){
    secondArray.push(someFunction(value));
  }
  this.myEach(pushChangedValue);
  return secondArray;
}

var timesTwoFunction = function(value) {
  return value * 2;
}

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

Array.prototype.myInject = function(someFunction) {
  accum = this[0]

  function alterValues(value) {
    accum = someFunction(accum, value)
  }

  this.slice(1).myEach(alterValues);
  return accum;
}

var add = function(accum, value) {
  return accum + value;
}

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

Related

  1. mul(value)
    Array.prototype.mul = function(value) {
      var newArray = new Array();
      for(var i=0; i<this.length; i++) {
        newArray[i] = this[i] * value;
        return newArray;
    
  2. mult(other)
    Array.prototype.mult = function(other) {
        if(isFinite(other))  
            other = new Array(this.length).fill(other);
        else
            console.assert(this.length == other.length, 'mult: len(a) != len(b)');
        var ret = new Array(this.length);
        for(var i = 0; i < ret.length; i++)
            ret[i] = this[i] * other[i];
        return ret;
    ...
    
  3. mult2()
    Array.prototype.mult2 = function() {
        for (var i = 0; i < this.length; i++) {
            this[i] = this[i] * 2;
    };
    
  4. 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;
    };
    ...
    
  5. 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;
    ...
    
  6. 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
    
  7. multiplyByTwo()
    Array.prototype.multiplyByTwo = function() {
      var newArr = [];
      for (var i = 0; i < this.length; i++) {
        newArr.push(this[i] * 2)
      return newArr;
    };
    console.log([1, 2, 3].multiplyByTwo());
    Array.prototype.myEach = function(func) {
    ...
    
  8. 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));
    };