Forum Moderators: open

Message Too Old, No Replies

totaling arrays

         

planedriver

7:47 pm on Dec 9, 2006 (gmt 0)

10+ Year Member



anyone have a quick way to loop through an array using javascript and return a total. the array is a form element index array all named the same such as hoursworked[] because the array is generated by a php while loop.

Bernard Marx

8:29 am on Dec 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




Array.prototype.getTotal = function()
{
var total = k = 0;
while(k<this.length)
total += this[k++];
return total;
}

var allHours = [1,2,3];

var total = allHours.getTotal();

// Assuming that all the values are actually numbers of course!