Results 1 to 4 of 4

Thread: looking to create javascript calculator

  1. #1
    Join Date
    Feb 2009
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default looking to create javascript calculator

    I am trying to create a basic javascript calculator

    the problem is that I can code it to add and minus or multiple and divide but not all four functions using the code I have below.

    thanks

    <html>
    <head>
    <title>Template</title>
    <script>

    function add(numID1, numID2)
    {
    var myString = "";
    var num1 = document.getElementById(numID1).value;
    num1 *= 1;

    var num2 = document.getElementById(numID2).value;
    num2 *= 1;

    op = document.getElementById('opChoice').value;

    if(op == 'divide')
    ans = num1 / num2;
    else
    ans = num1 * num2;
    myString = ans;
    document.getElementById('content').innerHTML = myString;

    }

    function pickOp(val)
    {
    if(val.length > 0)
    document.getElementById('opChoice').value = val;
    }
    </script>
    <link rel="stylesheet" href="style/layout.css" media="screen" />
    </head>
    <body>
    <div id="container">
    <div id="header">
    <br />
    Header info goes here
    </div>
    <div id="submenu">
    </div>
    <div id="navmenu">
    <input type="text" id="num1" value="0" /><br />
    <select onChange="pickOp(this.value);">
    <option value="" />
    <option value="divide" />Divide
    <option value="multiple" />Multiple
    </select>
    <input type="text" id="opChoice" value="divide" /><br />
    <input type="text" id="num2" value="0" /><br />
    <button onClick="add('num1', 'num2');">Equals</button><br />
    </div>
    <div id="content">
    Content goes here
    </div>
    </div>
    </body>
    </html>

  2. #2
    Join Date
    Feb 2009
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    to update I think I found the solution but have a problem implementing it,

    feel a

    else conditional statements or switch statements

    would work but its not working

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Here: http://dynamicdrive.com/forums/showthread.php?t=41865

    I think that you would benefit from a tutorial.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Sep 2008
    Posts
    119
    Thanks
    13
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by tomwaits4noman View Post
    I am trying to create a basic javascript calculator

    the problem is that I can code it to add and minus or multiple and divide but not all four functions using the code I have below.

    thanks

    <html>
    <head>
    <title>Template</title>
    <script>

    function add(numID1, numID2)
    {
    var myString = "";
    var num1 = document.getElementById(numID1).value;
    num1 *= 1;

    var num2 = document.getElementById(numID2).value;
    num2 *= 1;

    op = document.getElementById('opChoice').value;

    if(op == 'divide')
    ans = num1 / num2;
    else
    ans = num1 * num2;
    myString = ans;
    document.getElementById('content').innerHTML = myString;

    }

    function pickOp(val)
    {
    if(val.length > 0)
    document.getElementById('opChoice').value = val;
    }
    </script>
    <link rel="stylesheet" href="style/layout.css" media="screen" />
    </head>
    <body>
    <div id="container">
    <div id="header">
    <br />
    Header info goes here
    </div>
    <div id="submenu">
    </div>
    <div id="navmenu">
    <input type="text" id="num1" value="0" /><br />
    <select onChange="pickOp(this.value);">
    <option value="" />
    <option value="divide" />Divide
    <option value="multiple" />Multiple
    </select>
    <input type="text" id="opChoice" value="divide" /><br />
    <input type="text" id="num2" value="0" /><br />
    <button onClick="add('num1', 'num2');">Equals</button><br />
    </div>
    <div id="content">
    Content goes here
    </div>
    </div>
    </body>
    </html>

    innerHTML is of the devil. :|
    document.write is document.wrong

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
  •