We help IT Professionals succeed at work.
Get Started

xjs: how to get sum of value in for loop

badtz7229
badtz7229 asked
on
137 Views
Last Modified: 2017-03-06
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

Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE