Sum (Addition) JavaScript Code Sample

Mindwatering Incorporated

Author: Tripp Black

Created: 03/20/2007 at 10:42 PM

 

Category:
Notes Developer Tips
JavaScript

Following function assumes "a" is a text string of numbers to add together for a total.
The values in the string are separated with the delineator "strdelim" (e.g. comma).

function GetSum(a, strdelim) {
// sum the values of the string "a", using strdelim as delineator
// split the passed string into new array
var nums = a.split(strdelim);
var total = 0;
// loop and build total, converting strings to numbers on-the-fly
for(var i = 0; i < nums.length; i++) total += Number(nums[i]);
return total;
}

previous page