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>