PDA

Click to See Complete Forum and Search --> : Unexpected Result In My JavaScript Calculator


rino_2
Jun 28th, 2000, 01:18 AM
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.

noone
Jun 28th, 2000, 01:36 AM
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.

rino_2
Jun 28th, 2000, 02:12 AM
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.

noone
Jun 28th, 2000, 03:02 AM
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?

rino_2
Jun 28th, 2000, 03:32 AM
Thanks for all the help!