Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hiii all,
i have for loop which i calculate balance of user (The user would has more than one balance) .
in this loop i would to add the old balance with the new value of balance , all of these in java script.

i tried to write that line:

JavaScript
Balance_Sum += balance;


But, when i displayed "Balance_Sum" variable, it appears as

"UnDefinded 100.00"

i want to know how can i disable "undefined" word and display balance correctly.

Thanks
Posted
Comments
_Zorro_ 1-Aug-11 4:34am    
Please, paste the code you're using to display such message.

You need to convert balance to a number before adding it to Balance_Sum.
If you don't do that, the addition is taken as a concatenation rather than a numeric operation.
 
Share this answer
 
When you declare a variable it's value is set as "undefined": if you subsequently try to add a number to it, it is converted to a string "Undefined" and a string concatenation is performed.
Either, check if the variable has a value before you add to it:
JavaScript
if (balance!=undefined)
{
   ...
}
Or initialise it when you declare it:
JavaScript
var balance = 0;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900