Implementación:
Código Javascript
:
Ver original/**
* Suma los elementos de un array
*
* @author Aijoona
*/
Array.prototype.sum = function() {
if(Array.prototype.reduce) {
return this.reduce(function(a, b) {
return Number(a) + Number(b);
});
}
var s = 0, a = this.concat();
while(a.length) s += Number(a.shift());
return s;
};
[1,2,3,4,5].sum(); // 15