Results 1 to 5 of 5

Thread: Unexpected Result In My JavaScript Calculator

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 1999
    Location
    Leeds, UK
    Posts
    287

    Smile

    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.
    rino_2
    Visual Basic, HTML
    Please Visit my Site: Richard's VB Site

  2. #2
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    292
    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."

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 1999
    Location
    Leeds, UK
    Posts
    287

    Smile

    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.
    rino_2
    Visual Basic, HTML
    Please Visit my Site: Richard's VB Site

  4. #4
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    292
    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."

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 1999
    Location
    Leeds, UK
    Posts
    287

    Smile

    Thanks for all the help!
    rino_2
    Visual Basic, HTML
    Please Visit my Site: Richard's VB Site

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width