0

I have worked on this for hours and I can't get it to work...any help on the code appreciated! Thanks.

<html>
   <head>
         <title>
            My Calculator - Constance Dobbins
         </title>
             <script>
                 function getInputAsText(_id){return document.getElementById(_id).value}
                 function getInputAsNumber(_id){return           parseFloat(document.getElementById(_id).value)}
                 function setOutput(_id, _value){document.getElementById(_id).value = _value}
                 function calculate (code)
                  {
                     //declare variables
                       var first
                     var second
                     var resultAsNumber
       var resultAsText


                     //get variable's value
      first=getInputAsNumber("first")
      second=getInputAsNumber("second")
      if (code == 0)
        {
          resultAsNumber = first + second
          resultAsText = first + " + " + second + " = " + resultAsNumber
        }

      if (code == 1)
        {
          resultAsNumber = first -second
          resultAsText = first + " - " + second + " = " + resultAsNumber
        }

      if (code == 2)
        {
          resultAsNumber = first *second
          resultAsText = first + " * " + second + " = " + resultAsNumber
        }

      if (code == 3)
        {
          resultAsNumber = first /second
          resultAsText = first + " / " + second + " = " + resultAsNumber
        }

    //write output value
      setOutput ("resultAsText")
                 }
              </script> 
  </head>

   <body>
    Instructions:<br>
    Type a number in each box and click and choose sum, difference, product or quotient.<br>
    The answer of the two numbers will appear below.<br><br>

    Input Values:<br>
    First number to include: <INPUT id="first"><br>  
    Second number to include: <INPUT id= "second"> <br>

    <input type ="submit" value="sum" onclick=calculate (0)">
    <input type ="submit" value="difference" onclick=calculate (1)">
    <input type ="submit" value="product" onclick=calculate (2)">
    <input type ="submit" value="quotient" onclick=calculate (3)"><br><br>

    Output Values:<br>
    Result: <input id= "resultAsText" size="50" >

    </body>
</html>
3
Contributors
3
Replies
6
Views
4 Years
Discussion Span
Last Post by constance.dobbins
0

A few slight changes. main change I made was line 67 of your example.

<!DOCTYPE html>
<html>
<head>
<title>My Calculator - Constance Dobbins</title>
<script>
function getInputAsText(_id){return document.getElementById(_id).value}
function getInputAsNumber(_id){return           parseFloat(document.getElementById(_id).value)}
function setOutput(_id, _value){document.getElementById(_id).value = _value}
function calculate (code) {
      //declare variables
      var first;
      var second;
      var resultAsNumber;
      var resultAsText;
      //get variable's value
      first=getInputAsNumber("first");
      second=getInputAsNumber("second");
      if (code == 0) {
          resultAsNumber = first + second;
          resultAsText = first + " + " + second + " = " + resultAsNumber;
        }
      if (code == 1) {
          resultAsNumber = first -second;
          resultAsText = first + " - " + second + " = " + resultAsNumber;
        }
      if (code == 2) {
          resultAsNumber = first *second;
          resultAsText = first + " * " + second + " = " + resultAsNumber;
        }
      if (code == 3) {
          resultAsNumber = first /second;
          resultAsText = first + " / " + second + " = " + resultAsNumber;
        }
    //write output value
document.getElementById('resultAsText').value = resultAsText;
}
</script> 
  </head>
   <body>
    Instructions:<br>
    Type a number in each box and click and choose sum, difference, product or quotient.<br>
    The answer of the two numbers will appear below.<br><br>
    Input Values:<br>
    First number to include: <INPUT id="first"><br>  
    Second number to include: <INPUT id= "second"> <br>
    <input type ="submit" value="sum" onclick="calculate(0)">
    <input type ="submit" value="difference" onclick="calculate(1)">
    <input type ="submit" value="product" onclick="calculate(2)">
    <input type ="submit" value="quotient" onclick="calculate(3)"><br><br>
    Output Values:<br>
    Result: <input id= "resultAsText" size="50" >
</body>
</html>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.