Nodejs Array Average avarage()

Here you can find the source of avarage()

Method Source Code

Array.prototype.avarage = function()
{
    return this.sum() / this.length;
}
Array.prototype.sum = function()
{
    var sum = 0;/*from  w  w w . ja  va 2s . c  o m*/
    for(var i = this.length; -- i >= 0 ;)
    {
        sum += (this[i] === null || this[i] === undefined) ? 0 : this[i];
    }

    return sum;
}

Related

  1. average()
    Array.prototype.average = function() {
      var avg = 0;
      this.forEach(function(val) {
        avg += val;
      });
      return avg / this.length;
    };
    
  2. average()
    Array.prototype.average = function () {
      return this.sum() / this.length
    
  3. average()
    Array.prototype.average = function(){
      return this.reduce( (a,b) => a + b, 0) / this.length
    
  4. average()
    Array.prototype.average = function () {
      return this.sum() / this.length;
    };
    console.log([1, 2, 3, 4].average());