What is wrong with my java/html calculator?

John Madden

New member
I'm trying to make a calculator that uses forms for input and an alert box as output.

Here is what i have so far, but all that happens when you click submit is the forms are cleared:

<html>


<head>
<script type="text/javascript">



function Calculate(Atext, Btext, form)
{
if operation.value="plus"{
var A = parseFloat(Atext);
var B = parseFloat(Btext);
return A+B;
}
if operation.value="minus" {
var A =parseFload(Atext);
var B=parseFloar(Btext);
return A-B;
}
if operation.value="multiply"{
var A =parseFload(Atext);
var B=parseFloar(Btext);
return A*B;
}
else{
var A =parseFload(Atext);
var B=parseFloar(Btext);
return A/B;
}
}
</script>
<title>Calculator</title>
</head>


<body>
<form name="Calc" method="POST">
<INPUT TYPE="text" NAME="Input1" Size="16">
<select name="operation">
<option value="plus">+</option>
<option value="minus">-</option>
<option value="multiply">*</option>
<option value="divide">/</option>
</select>
<INPUT TYPE="text" NAME="Input2" Size="16">
<input type="submit" onClick="alert('The answer is'+Calculate(this.form.Input1.value, this.form.Input2.value, this.form)+'thank you')"/>

</form>

</body>
</html>

Why is this not working?
Note: the calculator needs to stay in that same input/output format.
Thanks mike, that helped.

I also did some reformatting so that instead of just returning an answer variable, it returns an alert box. Then I just have it run the script onClick.

I also fixed some typos(for parsefloat).

Now it just displays the addition of the values, no matter what the operation value is. Any ideas?
I also noticed that when I click the button the operation value is reset to + on the webpage.(all forms are reset once alert box is closed)
 
Back
Top