March 10th, 2013, 11:59 PM
I am trying to understand this function and I have the following code:
If variable temp=1 I expect the alert to produce 34 but it shows 331 instead. It concatenates instead of evaluating.
I tried the eval() function because
also produces 331 when the input is 1.
What am I doing wrong?
March 11th, 2013, 12:07 AM
Like with PHP, avoid using eval().
Your problem is that temp is a string, and adding a string to a number acts as string concatenation and not numeric addition.
Run the string through parseInt() first.
temp = parseInt("1");