Nodejs Array Mean mean()

Here you can find the source of mean()

Method Source Code

Array.prototype.mean = function(){ 
   return this.reduce(function(previousMean, currentValue, i){
      return previousMean + (1/(i + 1))*(currentValue - previousMean);
   });//from  w w  w .  j  a  va 2s.  com
};

Array.prototype.median = function() {
    var values = this;
    values.sort( function(a,b) {return a - b;} );
 
    var half = Math.floor(values.length/2);
 
    if(values.length % 2){
        return values[half];
    } else {
        return (values[half-1] + values[half]) / 2.0;
    }
};

Array.prototype.max = function(){
   return this.reduce(function(p,n){ 
      if(n > p || p === null){ 
         return n;
      } else {
         return p;
      }
   },null);
};

Array.prototype.min = function(){
   return this.reduce(function(p,n){ 
      if(n < p || p === null ){ 
         return n;
      } else {
         return p;
      }
   },null);
};

Array.prototype.sum = function(){
   return this.reduce(function(p,n){
      return p + n;
   }, 0);
};

Related

  1. mean()
    Array.prototype.mean = function() {
        return !this.length ? 0
            : this.reduce(function(pre, cur, i) {
                return (pre * i + cur) / (i + 1);
                });
    console.log( [1,2,3,4,5].mean() );   
    console.log( [].mean() );            
    
  2. mean()
    Array.prototype.mean = function () {
      var sum = this.reduce(
        function (prev, cur) {
          return prev + cur;
      );
      return sum / this.length;
    
  3. mean()
    Array.prototype.mean = function () {
        var sum = this.reduce(function(previousValue, currentValue) {
            return previousValue + currentValue;
        });
        return sum / this.length;
    };
    
  4. mean()
    Array.prototype.mean = function() {
        mean = 0;
        for (var i = 0; i < this.length; i++) {
            mean += this[i];
        mean = mean / this.length;
        return mean;
    };
    
  5. mean()
    Array.prototype.mean = function() {
      var length = this.length;
      return this.total()/length;
    };
    
  6. mean()
    Array.prototype.mean = function(){
        return this.sum()/this.length;
    Array.prototype.sum = function(){
      var ret = this[0];
      for(var i=1;i<this.length;i++){
        ret += this[i]
      return ret;
    ...