Click here to Skip to main content
14,640,651 members
Rate this:
Please Sign up or sign in to vote.
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:

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.
Rate this:
Please Sign up or sign in to vote.

Solution 1

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.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

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:
if (balance!=undefined)
{
   ...
}
Or initialise it when you declare it:
var balance = 0;
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100