troubleshooting Question

xjs: how to get sum of value in for loop

Avatar of badtz7229
badtz7229Flag for United States of America asked on
JavaScript
2 Comments1 Solution138 ViewsLast Modified:
I have the following for-loop and I want  sum of all strTotal to be stored in a variable as a number.
Thus far it keeps appending my value (i.e 0564 instead of simply 564).

var nodeFeeGroup = ViewRSDom.CreateElementNode("FeeGroup");

var nsSvc = Dom.SelectNode(Dom.Root, "Opt/Svc");
if (nsSvc)
{
	var sum = 0;
	for (var i = 0; i < nsSvc.Count; i++)
	{
		
		var nodeSvc = nsSvc.GetItem(i);
		if (nodeSvc)
		{
			var nodSvcPrice = nodeSvc.SelectSingle("SvcPrice");                    
			if (nodSvcPrice)
				var strTotal = nodSvcPrice.GetAttribute("Total");

			var nodeFee    = ViewRSDom.CreateElementNode("Fee");
			var nodeAmount = ViewRSDom.CreateElementNode("Amount");

			var textNode = ViewRSDom.CreateTextNode(strTotal);
			nodeAmount.AppendChild(textNode);

		}


		nodeFeeGroup.AppendChild(nodeFee);
		nodeFee.AppendChild(nodeAmount);

		sum =  sum + strTotal;  //I want to sum of all strTotal to be stored
	}//for


	nodeFeeGroup.SetAttribute("Amount", sum);
	nodeInfoGroup.AppendChild(nodeFeeGroup);

}
ASKER CERTIFIED SOLUTION
n2fc

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros