|
-
Jun 28th, 2000, 01:18 AM
#1
Thread Starter
Hyperactive Member
Hi Everybody,
I've just created a calculator in JavaScript. Everything is working just fine apart from the plus button. I have a number in the variable num1 and a number in the variable num2. When I say num1 += num2 the values of both are concatenated and placed into num1. What I would like to happen is the values of both be added to each other and sent to num1. Could you please help me? Thanks
You can see my calculator here http://members.xoom.com/rino_2/jscalc.htm it's not great, I'm new to JavaScript.
-
Jun 28th, 2000, 01:36 AM
#2
Hyperactive Member
Rino, I only get the following error while the page is loading, and then you calculator does not load:
JavaScript Error: http://members.xoom.com/_XMCM/rino_2/jscalc.htm,
line 279:
syntax error.
-->splay.value = ""
m1
{
ry is empty!")
+ number1.value)
)
..^
I am using Netscape 4.7
The problem you are describing sounds like it cause becuase your variables are strings
use the parseInt(x) function to return the number value of x in your calculation.
"People who think they know everything are a great annoyance to those of us who do."
-
Jun 28th, 2000, 02:12 AM
#3
Thread Starter
Hyperactive Member
Hi Noone,
I'm not sure why it's not working on your computer, I've tested it on more than one system and it works fine. I did as you said and changed the code:
num1 += num2
to
parseInt(num1) += parseInt(num2)
I'm getting an error now, what am I doing wrong? Thanks for the help.
-
Jun 28th, 2000, 03:02 AM
#4
Hyperactive Member
You probably want:
num1 = parseInt(num1) + parseInt(num2)
I'm not sure why it would be concenating it otherwise, are num1 and num2 coming straight from a textbox?
"People who think they know everything are a great annoyance to those of us who do."
-
Jun 28th, 2000, 03:32 AM
#5
Thread Starter
Hyperactive Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|