SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2004
    Location
    Australia
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    My calculator won't work. :(.

    Anyone care to debug this? Please. I suck at JS.


    <html>
    <title>Evaluation page</title>
    <head>
    <SCRIPT LANGUAGE = "JavaScript">
    <!-- begin hiding JavaScript
    function evaluate(){
    var first = eval(document.eval.value1.value)
    var second = eval(document.eval.value2.value)

    if (document.eval.evaluation.value == "Multiplication") {
    sum = first * second
    }
    if (document.eval.evaluation.value =="Addition") {
    sum = first + second
    }
    if (document.eval.evaluation.value =="Subtraction") {

    sum = first - second
    }
    if (document.eval.evaluation.value =="Division") {
    if (second = 0) {
    document.write (For division, you must put a value higher than 0 in the second box.".")
    } else {
    sum = first / second
    }
    }
    document.eval.answer.value = sum
    }
    //end hiding JavaScript-->
    </SCRIPT>

    </head>
    <body>
    <form name="eval">
    First Number: <input type="text" name="value1" size="10">
    <br>Second Number: <input type="text" name="value2" size="10">
    <br>Division/Subtraction/Multiplication/Addition: <select name="evaluation">
    <option>Addition</option>
    <option>Subtraction</option>
    <option>Multiplication</option>
    <option>Division</option>
    <br><input type="button" value="Evaluate my equation!" onclick="evaluate()">
    <input type="text" name="answer" size="10">
    </form>
    </body>
    </html>

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    function evaluate(){
    var first = eval(document.eval.value1.value)
    var second = eval(document.eval.value2.value)
    
    if (document.eval.evaluation.value == "Multiplication")
    ...
    ...
    <form name="eval">
    
    <select name="evaluation">

    Whoever wrote that should be very afraid--darwinism will get its vengeance.


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
  •