Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Calculator Help

  1. #1
    New to the CF scene
    Join Date
    May 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Calculator Help

    Hello there.

    I'm writing a very simple calculator that allows the user to input values so the "engine" (what I call it) can return a product. It uses .innerHTML to grab a form element's value and replace with the user-supplied variable.

    When I run the code, it returns NaN as a result. O_O I'm only familiar with basic Javascript, so this is about the limit of my programming knowledge. XD

    Despite that, my suspicions for why the code won't work lies with the 'engine'. I want it to parse user-supplied variables to return a proper result; it won't do this. Also, the engine should only parse integers and decimals. How can I set about doing both of these, so my calculator can work?

    Thanks in advance for your help!

    Code:
    <head>
    <script type="text/javascript">
    function exchangeRate()
    {
    var num1 = document.getElementById('num1').value; 
    var num2 = document.getElementById('num2').value;
    var num3 = document.getElementById('num3').value;
    document.getElementById('result').innerHTML = ((num1*100)/num2)*num3); //the engine 
    }
    </head>
    </script>
    
    <body>
    <p>The exchange rate is <b id='result'>null</b></p> 
    <form name="input">
    <input type='text' id='num1' value='Cash Here'/>
    <br>
    <input type='text2' id='num2' value='Exchange Rate Here'/>
    <br>
    <input type='text3' id='num3' value='Units Here'/>
    <br>
    <input type='button' onclick='exchangeRate()' value='Calculate'/>
    </form> 
    </body>
    Last edited by ThatGuy16; 05-29-2012 at 02:35 AM. Reason: Resolved!

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,980
    Thanks
    56
    Thanked 557 Times in 554 Posts
    I'm surprised it returns anything at all. You need to learn to use the debugger (Chrome has a good one).
    You have uneven brackets here:
    Code:
    ((num1*100)/num2)*num3);
    and your script closing tag is after your head tag.

    fix those and it works better. You can validate the input fields to make sure that only numbers are entered. This has been covered here and elsewhere endlessly - try searching and if you run into real problems, post back

  • Users who have thanked xelawho for this post:

    ThatGuy16 (05-29-2012)

  • #3
    New to the CF scene
    Join Date
    May 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by xelawho View Post
    I'm surprised it returns anything at all. You need to learn to use the debugger (Chrome has a good one).
    You have uneven brackets here:
    Code:
    ((num1*100)/num2)*num3);
    and your script closing tag is after your head tag.

    fix those and it works better. You can validate the input fields to make sure that only numbers are entered. This has been covered here and elsewhere endlessly - try searching and if you run into real problems, post back
    It works! Thanks.
    Last edited by ThatGuy16; 05-29-2012 at 02:32 AM.

  • #4
    Regular Coder low tech's Avatar
    Join Date
    Dec 2009
    Posts
    852
    Thanks
    173
    Thanked 94 Times in 94 Posts
    Hi

    It works!
    That's great

    tip: now look into

    Number()
    parseFloat()
    parseInt()

    LT
    "The greatest revenge is to accomplish what others say you cannot do."
    ~ Unknown

    I used to be indecisive, but now I'm not so sure.

  • Users who have thanked low tech for this post:

    ThatGuy16 (05-29-2012)

  • #5
    New to the CF scene
    Join Date
    May 2012
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by low tech View Post
    Hi



    That's great

    tip: now look into

    Number()
    parseFloat()
    parseInt()

    LT
    Sounds like good advice. I'll check it out.


  •  

    Tags for this Thread

    Posting Permissions

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