A simple calculator written in javascript, with a lot of content and practical methods. Recommended _ javascript skills

Source: Internet
Author: User
Recently I used javascript to write a simple calculator. It seems okay to test it myself. The Code has been commented out. It is very good. I suggest you learn it. Recently I wrote a simple calculator in javascript, and it feels okay to test it on my own. Let's take a look at the interface:

The interface is like this, but what is the function?

Now it's just a simple standard calculator that can perform addition, subtraction, multiplication, division, and remainder operations. If the divisor is zero, the following prompt is displayed, as shown in the following figure:

I don't know how to write it, but for new users, this is definitely a big meal. There are a lot of things I can come into use to learn. If you see any omissions or errors, please give us some advice.

Paste the code below, and hope there are enough comments in it.
Js section:

The Code is as follows:


Var num = 0, result = 0, numshow = "0 ";
Var operate = 0; // indicates the input status.
Var calcul = 0; // indicator of the computing status
Var quit = 0; // prevents duplicate button flag
Function command (num ){
Var str = String (document. calculator. numScreen. value); // obtain the currently displayed data
Str = (str! = "0 ")? (Operate = 0 )? Str: ""): ""; // if the current value is not "0" and the status is 0, the current value is returned. Otherwise, a null value is returned;
Str = str + String (num); // append a character to the current value
Document. calculator. numScreen. value = str; // refresh the display
Operate = 0; // reset the input status
Quit = 0; // reset the flag to prevent repeated buttons
}
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, return when str + "00 ", otherwise, "0" is returned ";
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, the current value is returned; otherwise, the system returns "0 ";
For (I = 0; I <= str. length; I ++) {// you can check whether a dot is displayed.
If (str. substr (I, 1) = ".") return false; // if yes, no insert
}
Str = str + ".";
Document. calculator. numScreen. value = str;
Operate = 0;
}
Function del () {// return
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 () {// Addition
Calculate (); // call the calculation function
Operate = 1; // change the input status
Calcul = 1; // change the computing status to add
}
Function minus () {// Subtraction
Calculate ();
Operate = 1;
Calcul = 2;
}
Function times () {// Multiplication
Calculate ();
Operate = 1;
Calcul = 3;
}
Function pide () {// Division
Calculate ();
Operate = 1;
Calcul = 4;
}
Function persent () {// returns the remainder
Calculate ();
Operate = 1;
Calcul = 5;
}
Function equal (){
Calculate (); // equal
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 operation count is zero and the anti-repeated button status
Switch (calcul) {// determines 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 divisor cannot be zero! "; SetTimeout (clearnote, 4000)} break;
Case 5: result = num % numshow; break;
}
Quit = 1; // avoid repeated buttons
}
Else {
Result = numshow;
}
Numshow = String (result );
Document. calculator. numScreen. value = numshow;
Num = result; // store the current value
}
Function clearnote () {// clearing prompt
Document. getElementById ("note"). innerHTML = "";
}


Html section:

The Code is as follows:






Writing to beginners: js form Operations (4) simple calculator (2)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.