I recently wrote a simple calculator using javascript. I tested it myself and it felt fine. Let me show you the interface first:
The interface is like this, but what about the functions?
Now it is just a simple standard calculator that can perform continuous operations of addition, subtraction, multiplication and division, as well as remainder operations. If a zero dividend error occurs, a prompt will be given below, like this:
I don’t know how it is written, but for novices, this is definitely a big meal, and there are many things that can be accessed and learned. If there are any experts who see any omissions or mistakes in it, I hope they will give you some advice and pointers.
Paste the code below, I hope there are enough comments inside.
js part:
var num=0,result =0,numshow="0";
var operate=0; //Flag to judge input status
var calcul=0; //Flag to judge calculation status
var quit=0; //Prevent Flag of repeated keystrokes
function command(num){
var str=String(document.calculator.numScreen.value); //Get the current display data
str=(str!="0") ? ((operate==0) ? str : "") : ""; //If the current value is not "0" and the status is 0, return the current value, otherwise return a null value;
str=str String( num); //Append characters to the current value
document.calculator.numScreen.value=str; //Refresh the display
operate=0; //Reset the input status
quit=0; //Reset Set the flag to prevent repeated key presses
}
function dzero(){
var str=String(document.calculator.numScreen.value);
str=(str!="0") ? ( (operate==0) ? str "00" : "0") : "0"; //If the current value is not "0" and the status is 0, then return str "00", otherwise return "0";
document.calculator.numScreen.value=str;
operate=0;
}
function dot(){
var str=String(document.calculator.numScreen.value);
str=(str!="0") ? ((operate==0) ? str : "0") : "0"; //If the current value is not "0" and the status is 0, return the current value, otherwise return "0";
for(i=0; i<=str.length;i ){ //Determine whether there is already a dot
if(str.substr(i,1)= =".") return false; //If there is, no more inserting
}
str=str ".";
document.calculator.numScreen.value=str;
operate=0;
}
function del(){ //Backspace
var str=String(document.calculator.numScreen.value);
str=(str!="0") ? str : " ";
str=str.substr(0,str.length-1);
str=(str!="") ? str : "0";
document.calculator.numScreen.value= str;
}
function clearscreen(){ //Clear data
num=0;
result=0;
numshow="0";
document.calculator.numScreen. value="0";
}
function plus(){ //Add
calculate(); //Call calculation function
operate=1; //Change input status
calcul= 1; //Change the calculation status to addition
}
function minus(){ //Subtraction
calculate();
operate=1;
calcul=2;
}
function times(){ //Multiplication
calculate();
operate=1;
calcul=3;
}
function divide(){ //Division
calculate ();
operate=1;
calcul=4;
}
function present(){ //Find remainder
calculate();
operate=1;
calcul=5;
}
function equal(){
calculate(); //Equal to
operate=1;
num=0;
result=0;
numshow="0";
}
//
function calculate(){
numshow=Number(document.calculator.numScreen.value);
if(num!=0 && quit !=1){ //Determine whether the previous operand is zero and the status of anti-repeat keystrokes
switch(calcul){ //Determine the input status
case 1:result=num numshow;break; // Calculate " "
case 2:result=num-numshow;break; //Calculate "-"
case 3:result=num*numshow;break;
case 4:if(numshow!=0) {result=num/numshow;}else{document.getElementById("note").innerHTML="The dividend cannot be zero! "; setTimeout(clearnote,4000)} break;
case 5:result=num%numshow;break;
}
quit=1; //Avoid repeated key presses
}
else{
result=numshow;
}
numshow=String(result);
document.calculator.numScreen.value=numshow;
num=result; //Storing the current value
}
function clearnote(){ //Clear the prompt
document.getElementById("note").innerHTML="";
}
html part: