Skip to content

Instantly share code, notes, and snippets.

@jdlich
Created August 2, 2011 22:20
Show Gist options
  • Save jdlich/1121375 to your computer and use it in GitHub Desktop.
Save jdlich/1121375 to your computer and use it in GitHub Desktop.
Array.prototype.sum = function() {
var sum = 0;
for ( var i = 0; i < this.length; i++ ) {
sum += this[i];
}
return sum;
}
@bsparks
Copy link

bsparks commented Aug 2, 2011

if it's supported in the browser you could use reduce. here is a link with tutorial and prototype in case the browser doens't have it.
http://www.tutorialspoint.com/javascript/array_reduce.htm

basically:

var arr = [1,2,3,33];
arr.reduce(function(a,b) { return a + b; });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment