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>

Recommended Answers

All 3 Replies

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>

@jorgeM - thanks for checking it out for me. I appreciate it!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.