Javascript Array avg()

Description

Javascript Array avg()


Array.prototype.avg = function(){
  var tl = this.sum();
  return tl/this.length;
};

Javascript Array avg()


Array.prototype.avg = function(){
 return this.reduce(function(c,p){ return c + p; })/this.length;
};

Javascript Array avg()


// Takes a single-dimensional array of numbers and returns the average of those numbers.
Array.prototype.avg = function() {
  var av, num, _i, _len;
  av = 0;/* w  w  w . java2s .c o m*/
  for (_i = 0, _len = this.length; _i < _len; _i++) {
    num = this[_i];
    av = av + num;
  }
  return av / this.length;
};



PreviousNext

Related