Nodejs Array Square square()

Here you can find the source of square()

Method Source Code

Array.prototype.square = function (){
  var result = this.map(function(v){
      return v*v;
  });//from w  w  w.  ja  va 2  s.c o  m
  return result;
};

Array.prototype.cube = function (){
  var result = this.map(function(v){
    return v*v*v;
  });
  return result;
};

Array.prototype.sum = function (){
  var result = new Number();
  this.forEach(function(v){
    result += v;
  });
  return result;
};

Array.prototype.average = function (){
  var result = new Number();
  this.forEach(function(v){
    result += v;
  });
  return result / this.length;
};

Array.prototype.even = function (){
  var result = new Array();
  this.forEach(function(v){
    if(v%2 == 0){
      result.push(v);
    }
  });
  return result;
};

Array.prototype.odd = function (){
  var result = new Array();
  this.forEach(function(v){
    if(v%2 != 0 && v%1 == 0){
      result.push(v);
    }
  });
  return result;
};

Related

  1. square()
    Array.prototype.square = function(){
        return this.map((element) =>  Math.pow(element,2));}
    ==
    Array.prototype.cube = function(){
        return this.map((element) =>  Math.pow(element,3));}
    
  2. square()
    Array.prototype.square = function () {
      return this.map(function(n) { 
        return n * n; 
      });
    
  3. square()
    Array.prototype.square = function () {
      return this.map(function (e) {
        return e * e;
      });
    };
    
  4. square()
    Array.prototype.square = function () {
      return this.map((item) => {
        return Math.pow(item, 2);
      });
    };
    
  5. square()
    Array.prototype.square = function(){
        return this.map(function(e){
            return (e * e);
        });
    };
    Array.prototype.cube = function(){
        return this.map(function(e){
            return (e * e * e);
        });
    ...
    
  6. square()
    Array.prototype.square = function () { return this.map(elem => elem*elem); }
    Array.prototype.cube = function() { return this.map(elem => Math.pow(elem, 3)); }
    Array.prototype.average = function () { return this.length > 0 ? this.reduce((a,b) => a+b) / this.length : NaN;  }
    Array.prototype.sum = function () { return this.reduce((a,b) => a+b); }
    Array.prototype.even = function () { return this.filter(elem => elem % 2 === 0); }
    Array.prototype.odd = function () { return this.filter(elem => elem % 2 > 0); }
    
  7. square()
    Array.prototype.square  = function () { return this.map(function(n) { return n*n; }); };
    Array.prototype.cube    = function () { return this.map(function(n) { return n*n*n; }); };
    Array.prototype.average = function () { return this.sum() / this.length; };
    Array.prototype.sum     = function () { return this.reduce(function(a, b) { return a + b; }, 0); };
    Array.prototype.even    = function () { return this.filter(function(item) { return 0 === item % 2; }); };
    Array.prototype.odd     = function () { return this.filter(function(item) { return 0 !== item % 2; }); };
    
  8. square()
    Array.prototype.square = function()
      var ret = new Array();
      this.forEach(function(a) {
        ret.push(a * a);
      });
      return ret;
    
  9. square()
    Array.prototype.square = function(){
      var newArr = [];
      for (var i of this){
        newArr.push(i*i);
      return newArr;
    };