Today I was bored and wanted to write something, but suddenly I thought of writing a calculator in JavaScript. There are still many bugs in the program. I will record it here first and then slowly correct it later.
The code is as follows:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>javascript code to implement simple calculator_Wulin.com</title><style type="text/css">input{width:30px;height:20px;text-align:center;}#tbCalculator td{text-align:center;vertical-align:middle;}</style><script type="text/javascript">var result; //Save the numeric value in the input box before the click operator; //Save the operator var isPressEqualsKey = false; //Record whether to press or not"="key//Number key event function connectionDigital(control){var txt = document.getElementById('txtScream');if(isPressEqualsKey){ txt.value = ""; //The calculation has been performed, then clear the numerical input box and start again isPressEqualsKey = false;}//The decimal point already exists in the numerical input, then the decimal point is not allowed to be entered if(txt.value.indexOf('.') > -1 && control.value == '.')return false;txt.value += control.value; //Assign the control value to the numerical input box}//Backspace event function backspace(){var txt = document.getElementById('txtScream');txt.value = txt.value.substring(0,txt.value.length - 1);}//ce key event: clear the numeric input box function clearAll(){document.getElementById('txtScream').value = "";result = "";operator = "";}// +, -, *, / Event function calculation(control){//Save the operator in the global variable operator = control.value; var txt = document.getElementById('txtScream');if(txt.value == "")return false; //There is no number in the numerical input box, then the operator cannot be entered//Save the value in the numerical input box into the calculation expression result = txt.value; //Clear the input box to be entered to the operation value txt.value = ""; }//The calculation result function getResult(){var opValue;//The operator var sourceValue = parseFloat(result);var txt = document.getElementById('txtScream');if(operator == '*')opValue = sourseValue * parseFloat(txt.value);else if(operator == '/')opValue = sourseValue / parseFloat(txt.value);else if(operator == '+')opValue = sourseValue + parseFloat(txt.value);else if(operator == '-')opValue = sourseValue - parseFloat(txt.value);txt.value = opValue;isPressEqualsKey = true;result = "";opValue = "";}</script></head><body><table id="tbCalculator" align="center" cellpadding="0" cellpacing="0" bordercolor="#0066FF"><tr><td colspan="4" align="center"><input type="text" name="txtScream" id="txtScream" readonly="readonly" /> </td></tr><tr><td colspan="2"><input type="button" name="btnCE" id="btnCE" value="CE" align="right"; onclick="clearAll();" /></td><td colspan="2"><input type="button" name="btn10" id="btn10" value="Backspace" align="right"; onclick="backspace();" /></td></tr><tt><td><input type="button" name="btn7" id="btn7" value="7" onclick="connectionDigital(this);" /></td><td><input type="button" name="btn8" id="btn8" value="8" onclick="connectionDigital(this);"/></td><td><input type="button" name="btn9" id="btn9" value="9" onclick="connectionDigital(this);" /></td><td><input type="button" name="btn6" id="btn6" value="/" onclick="calculation(this);" /></td></tr><tt><td><input type="button" name="btn4" id="btn4" value="4" onclick="connectionDigital(this);"/></td><td><input type="button" name="btn5" id="btn5" value="5" onclick="connectionDigital(this);"/></td><td><input type="button" name="btn6" id="btn6" value="6" onclick="connectionDigital(this);"/></td><td><input type="button" name="btn13" id="btn13" value="*" onclick="calculation(this);" /></td></tr><td><td><input type="button" name="btn1" id="btn1" value="1" onclick="connectionDigital(this);"/></td><td><input type="button" name="btn2" id="btn2" id="btn2" value="2" onclick="connectionDigital(this);"/></td><td><input type="button" name="btn3" id="btn3" value="3" onclick="connectionDigital(this);"/></td><td><input type="button" name="btn18" id="btn18" value="-" onclick="calculation(this);" /></td></tr><tr><td><input type="button" name="btn0" id="btn0" value="0" onclick="connectionDigital(this);" /></td><td><input type="button" name="btndot" id="btndot" value="." onclick="connectionDigital(this);" /></td><td><input name="btn22" type="button" id="btn22" value="=" onclick="getResult();" /></td><td><input type="button" name="btn23" id="btn23" value="+" onclick="calculation(this);" /></td></tr></table></body></html>The above code for implementing simple calculator in JavaScript is all the content I have shared with you. I hope you can give you a reference and I hope you can support Wulin.com more.