Results 1 to 2 of 2

Thread: [SOLVED] Require a JavaScript Calculator correction!

  1. #1
    Join Date
    Feb 2008
    Location
    Munster, Ireland
    Beans
    2,287
    Distro
    Xubuntu

    [SOLVED] Require a JavaScript Calculator correction!

    Hi,
    I need this calculator to add two numbers. They don't add them, but instead concatenate them.
    What should I do? Thanks.

    Code:
    <html>
    
    <head><title>JS</title>
    </head>
    
    <body>
    
    <script type="text/javascript">
    
    var numb1=prompt("number 1: ");
    var numb2=prompt("number 2: ");
    document.write(numb1 + numb2);
    
    </script>
    
    </body>
    
    </html>
    1st Distro used (live CD): Knoppix in early 2007 ¦ 1st Distro Installed: Ubuntu 7.10 in Feb 2008
    GNU/Linux User #470660 – Ubuntu User #28226
    Isaac Asimov: "I do not fear computers. I fear the lack of them."

  2. #2
    Join Date
    May 2008
    Location
    Germany
    Beans
    3
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: Require a JavaScript Calculator correction!

    You need to convert the information you gathered from the prompts to integers; then the '+' operator will do an addition. In the following snippet the parseInt() function acts on the input from the prompt() and your variables are treated as numbers then:

    Code:
    var numb1=parseInt(prompt("number 1: "));
    var numb2=parseInt(prompt("number 2: "));

Tags for this Thread

Bookmarks

Posting Permissions

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