Hosting Solutions
mobile_logo Table of Contents
seal_frontseal_back

average

The following custom extension to the Array object is courtesy of the author. This average() method can be added via prototype to the Array object interface so that all arrays in a script can call it like a built-in method.

Syntaxarray.average()

This average() method computes the mathematical average of an array of numbers.

Prototype the custom method(include this just once into your program upon start): Array.prototype.average = function(){ var sum = 0, avg, i = 0; for( i; i < this.length; i++ ){ sum += this[i]; } avg = sum / this.length; return avg; } Use the custom method on numeric arrays from that point on : var values = [ 98, 82, 70, 91 ]; alert( values.average() );

JavaScript

Methods

addEventListeneralertaveragebackblurconcatcreatedecodeURIdecodeURIComponentencodeURIencodeURIComponentevaleveryfilterforwardgoisArrayisFiniteisNaNjoinparseFloatparseIntpoppushpushStatereducereplaceStatereverseshiftshuffleslicesomesortspliceunshiftwrite