SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Calculator

Hybrid View

  1. #1
    Spam Can Kill!!! Disk-t's Avatar
    Join Date
    Jul 2001
    Location
    n/a
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calculator

    I can't seem to get the subtraction, multiplication, and division functions to work:

    <script>
    function getit() {
    if (document.former.oper.value == "+") {add()}
    else if (document.former.oper.value == "-") {subtract()}
    else if (document.former.oper.value == "*") {multiply()}
    else if (document.former.oper.value == "/") {divide()}
    }

    function add() {
    var num1 = document.former.num1.value;
    var num2 = document.former.num2.value;
    document.former.answer.value=parseInt(num1) + parseInt(num2)

    }
    function subtract() {
    var num1 = document.former.num1.value;
    var num2 = document.former.num2.value;
    document.former.answer.value=((num1) - (num2))

    }
    function multiply() {
    var num1 = document.former.num1.value;
    var num2 = document.former.num2.value;
    document.former.answer.value=((num1) * (num2))

    }
    function divide() {
    var num1 = document.former.num1.value;
    var num2 = document.former.num2.value;
    document.former.answer.value=((num1) / (num2))

    }

    </script>
    <form name="former">
    <input type="text" name="num1">

    <select size="1" name="oper">
    <option selected value="+">+</option>
    <option>-</option>
    <option>*</option>
    <option>/</option>
    &nbsp;
    </select><input type="text" name="num2">
    <input type="text" name="answer">
    <input type="button" value="submit" onClick="getit()">

    </form>

  2. #2
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's prolly because you don't have parseInt in those, but you do for addition.
    ck :: bringing chris to the masses.

  3. #3
    Spam Can Kill!!! Disk-t's Avatar
    Join Date
    Jul 2001
    Location
    n/a
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I thought parseInt was only needed for addition, because '+' is also used when writing sentences from different forms. Combining them instead of adding.

  4. #4
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahhh, it's because of the way you're accessing the <select>. It should be like this:

    var o = document.former.oper;
    var oper = o.options[o.selectedIndex].value;
    ck :: bringing chris to the masses.

  5. #5
    Spam Can Kill!!! Disk-t's Avatar
    Join Date
    Jul 2001
    Location
    n/a
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just did it using radio buttons. Much easier.


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
  •