This example describes the code for JavaScript to write a simple calculator. Share to everyone for your reference. Specifically as follows:
The screenshot of the running effect is as follows:
The specific code is as follows
JS Code:
<script>
var num = 0;//define the first input data
function jsq (num) {
//Get the current input
if (num== "%") {
document.getElementById (' Screenname '). Value=math.round (document.getElementById (' screenname '). Value)/100;
} else{
document.getElementById (' screenname '). Value + = document.getElementById (num). value
;
}
function Eva () {
//Calculate input result
document.getElementById ("Screenname"). Value = eval (document.getElementById (" Screenname "). value);
}
function Clearnum () {
//Clear 0
document.getElementById ("Screenname"). Value = null;
document.getElementById ("Screenname"). focus ();
}
function Tuige () {
//backspace
var arr = document.getElementById ("screenname");
Arr.value = arr.value.substring (0, arr.value.length-1);
}
</script>
A simple calculator is the way to achieve, you can also use JavaScript to write a calculator, hope to help you learn.