Javascript Array avarage()

Description

Javascript Array avarage()

Array.prototype.avarage = function()
{
    return this.sum() / this.length;
}

Javascript Array average()



Array.prototype.average = function() {
  var avg = 0;/*from  w w w. j av  a2 s.  c om*/
  this.forEach(function(val) {
    avg += val;
  });
  return avg / this.length;
};

Javascript Array average()


Array.prototype.average = function () {
    var len = this.length;
    if (len == 0)
        return 0;
    return this.sum() / len;
};

Javascript Array average()


Array.prototype.average = function(){
  return this.reduce((a,b)=>a+b,0) / this.length;
}

Javascript Array average()

Array.prototype.average = function () {
  return this.sum() / this.length
}

Javascript Array average()

Array.prototype.average = function(){
   var arrLen = this.length;
   if( arrLen == 0) return NaN;
   else return this.reduce((a,b)=> a+b,0)/arrLen;
}

Javascript Array average()

Array.prototype.average = function () { 
  return this.sum() / this.length; 
}
let arr=[1,2,3,4,5];
console.log(arr.average());/*from   www  . j a v  a  2 s  . c o  m*/

Javascript Array average()


Array.prototype.average = function () {
  return (!this.length) ? NaN : this.reduce((acc, value) => {
    return acc + value;
  }) / this.length;/*from  w w  w .  ja  v  a 2  s .  co  m*/
};

Javascript Array average()

Array.prototype.average = function (){
  var result = new Number();
  this.forEach(function(v){
    result += v;/*w w w .ja  va 2s .  co  m*/
  });
  return result / this.length;
};

Javascript Array average()

Array.prototype.average = function(){
  return this.reduce(function(sum, element){
    return sum += element;
  }, 0)/this.length;/*from   w  w  w. java2  s  . c om*/
};



PreviousNext

Related