Avatar of badtz7229
badtz7229
Flag for United States of America

asked on 

xjs: how to get sum of value in for loop

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);

}

Open in new window

JavaScript

Avatar of undefined
Last Comment
badtz7229

8/22/2022 - Mon