Javascript Array mean()
Array.prototype.mean = function() { return !this.length ? 0 : this.reduce(function(pre, cur, i) { return (pre * i + cur) / (i + 1); });//from w w w.j av a 2s. c om } alert( [1,2,3,4,5].mean() ); // 3 alert( [].mean() ); // 0
Array.prototype.mean = function(){ return this.reduce(function(previousMean, currentValue, i){ return previousMean + (1/(i + 1))*(currentValue - previousMean); });/*from w w w.j a v a 2 s. c o m*/ };
Array.prototype.mean = function () { var sum = this.reduce(function(previousValue, currentValue) { return previousValue + currentValue; });/*from w ww . ja va 2 s. c o m*/ return sum / this.length; };
Array.prototype.mean = function() { mean = 0;//from w w w .ja v a 2 s . c om for (var i = 0; i < this.length; i++) { mean += this[i]; } mean = mean / this.length; return mean; }; Array.prototype.total = function() { sum = 0; for (var i = 0; i < this.length; i++) { sum += this[i]; } return sum; }; Array.prototype.median = function() { var middle = Math.floor(this.length / 2); var evenMiddle = (this[middle-1] + this[middle]) / 2; if (this.length % 2 == 0) { return evenMiddle; } else { return this[middle]; } } Array.prototype.mode = function() { freq = {}; for (var i = 0; i < this.length; i++) { var curEl = this[i]; freq[curEl] ? freq[curEl]++ : freq[curEl] = 1; } var max = 1; for (key in freq) { if (freq[key] > max) { max = freq[key]; maxObj = {}; maxObj[key] = max; } } return maxObj; }