get the sum of all elements in an array





-10
Date Submitted Fri. Aug. 18th, 2006 1:16 AM
Revision 1 of 1
Syntax Master sundaramkumar
Tags JavaScript
Comments 2 comments
This function will add all the elements in an array and return total(sum)


Array.prototype.sum = function(){
        for(var i=0,sum=0;i<this.length;sum+=this[i++]);
        return sum;
}

 
call this as alert(yourarray.sum())

Comments

Comments Useless
Sun. Oct. 29th, 2006 12:06 PM    Scripter SCoon
Comments Example
Fri. Aug. 18th, 2006 5:51 AM    Syntax Master sundaramkumar

Voting