Note: the problem seems to be mainly caused by the program being unable to call the functions correctly, as changing them to just be 'alert()' made them all work fine.
I recently wanted to make a calculator on an html page using javascript, which was going fine until I noticed that after I finished, the buttons didn't actually do anything. After some troubleshooting (that's why the alert()s are in the functions, I figured out that my functions weren't being correctly called. I'm not quite sure what I'm doing wrong, so I need some help. Here's the code:
<html>
<head>
<title>
Web calculator
</title>
<script language="JavaScript">
<!--
var num1="", num2="", operation="", solution=0;
function clear(){
alert()
window.document.calculator.display.value="CLEAR";
window.document.calculator.display.size=5;
operation="";
solution=0;
num1="";
num2="";
}
function addNumber(number){
alert();
window.document.calculator.display.value+=number;
window.document.calculator.display.size+=1;
if(operation.length>0){
num2+=number;
}
else if(operation.length==0){
num1+=number;
};
}
function addOperation(op){
alert()
operation=op;
var value = window.document.calculator.display.value;
if((parseInt(value.indexOf('+')) + parseInt(value.indexOf('-')) + parseInt(value.indexOf('/')) + parseInt(value.indexOf('*')))!=-4){
window.document.calculator.display.size+=1;
window.document.calculator.display.value+=operation;
else{
window.document.calculator.display.value-="-";
window.document.calculator.display.value-="+";
window.document.calculator.display.value-="/";
window.document.calculator.display.value-="*";
window.document.calculator.display.value=num1+operation+num2;
};
}
function solve(){
alert()
num1=num1.parseFloat(num1);
num2=num2.parseFloat(num2);
switch(operation){
('+'){
solution=num1+num2;
}
('-'){
solution=num1-num2;
}
('*'){
solution=num1*num2;
}
('/'){
solution=num1/num2;
}
};
window.document.calculator.display.value=solution;
}
-->
</script>
</head>
<body onLoad="clear();">
<br><br><br><br><br><br><br><br>
<center>
<form name="calculator">
<table border=5>
<tr>
<center>
<td colspan=3>
<input type="text" name="display" readonly value="" size="5">
</td>
<td>
<input type="button" name="buttonclear" readonly value="C" onClick="clear();">
</td>
</center>
</tr>
<tr>
<center>
<td>
<input type="button" name="buttonseven" readonly value="7" onClick="addNumber('7');">
</td>
<td>
<input type="button" name="buttoneight" readonly value="8" onClick="addNumber('8');">
</td>
<td>
<input type="button" name="buttonnine" readonly value="9" onClick="addNumber('9' /*this part is fixed due to a below comment, however it was not the culprit of the problem*/);">
</td>
<td>
<input type="button" name="buttonminus" readonly value="-" onClick="addOperation('-');">
</td>
</center>
</tr>
<tr>
<center>
<td>
<input type="button" name="buttonfour" readonly value="4" onClick="addNumber('4');">
</td>
<td>
<input type="button" name="buttonfive" readonly value="5" onClick="addNumber('5');">
</td>
<td>
<input type="button" name="buttonsix" readonly value="6" onClick="addNumber('6');">
</td>
<td>
<input type="button" name="buttonplus" readonly value="+" onClick="addOperation('+');">
</td>
</center>
</tr>
<tr>
<center>
<td>
<input type="button" name="buttonone" readonly value="1" onClick="addNumber('1');">
</td>
<td>
<input type="button" name="buttontwo" readonly value="2" onClick="addNumber('2');">
</td>
<td>
<input type="button" name="buttonthree" readonly value="3" onClick="addNumber('3');">
</td>
<td>
<input type="button" name="buttontimes" readonly value="*" onClick="addOperation('*');">
</td>
</center>
</tr>
<tr>
<center>
<td>
<input type="button" name="buttondot" readonly value="." onClick="addNumber('.');">
</td>
<td>
<input type="button" name="buttonzero" readonly value="0" onClick="addNumber('0');">
</td>
<td>
<input type="button" name="buttonequals" readonly value="=" onClick="solve();">
</td>
<td>
<input type="button" name="buttondivide" readonly value="/" onClick="addOperation('/');">
</td>
</center>
</tr>
</table>
</form>
</center>
</body>
</html>