DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Array.prototype.sum,max,min

09.29.2005
| 32628 views |
  • submit to reddit
        
Array.prototype.sum = function(){
	for(var i=0,sum=0;i<this.length;sum+=this[i++]);
	return sum;
}
Array.prototype.max = function(){
	return Math.max.apply({},this)
}
Array.prototype.min = function(){
	return Math.min.apply({},this)
}

numeric value only.

[1,2,3].sum() => 6
[1,2,3].max() => 3
[1,2,3].min() => 1