JQuery Syntax

I have a piece of JQuery that loops.  Depending upon how many returns are on a page determines how many time the JQuery Loops.  What the script does is adds up numbers for each specific iteration of the loop.

So, if there were three iterations of the loop I may have a return of  10, 5 and 13.

The whole script is too long to put on here but the end looks like this:
	if(premium_total < 100) {
					premium = 100;
				} else {
					premium = premium_total;
				}
 	alert(premium);
 			$( "#result" ).html( premium );
 			$( "#price{row_id}" ).val( premium );	
 	//  END PRINT Final Price		

Open in new window


So after the script has looped, I want to take the total of all of the "Premium" for a final total.  So in my example here, my total will be 28.

I need something like:
var finalTotal = TOTAL of all of the Premium Vars

Open in new window

LVL 7
rgranlundAsked:
Who is Participating?
 
Big MontyConnect With a Mentor Senior Web Developer / CEO of ExchangeTree.org Commented:
try initializing the variable to 0:

var finalTotal = 0;
0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
at the end of your loop, just put:

finalTotal += premium;

remember to declare finalTotal above the loop first!
0
 
rgranlundAuthor Commented:
@BigMonty  I don't understand what you mean by "remember to declare finalTotal above the loop first! "
0
 
Big MontySenior Web Developer / CEO of ExchangeTree.org Commented:
before your loop code, declare the variable like:

var finalTotal;
0
 
rgranlundAuthor Commented:
OK.  I got that now.  Thanks./  However, when I run the code, the var premium gives me a number value, but:
finalTotal += premium;
alert(finalTotal);
The alert gives me NaN.

Not sure what I am doing wrong.  I have researched but I am still too much of a JQuery Newbie to figure it out.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.