The following small series will bring you a javascript simple calculator code editor that feels quite good. Now I will share it with you and give you a reference. Let's take a look at it. Today, I am bored. I want to write something and suddenly want to write a calculator using javascript. There are still a lot of bugs
JavaScript implementation Simple Calculator instance code this article mainly introduces the JavaScript implementation of simple calculator instance code of the relevant information, the need for friends can refer to the following
1234567891011121314151617181920212223242526272829303132333435363738394041424
1. Difficulty, how to get the value of the tag, note that the obtained value is a string type, but also to convertvar num1 = parseint (document.getElementById ("NUM1"). Value);var num2 = parseint (document.getElementById ("num2"). Value);2. How to get the value under the Select drop-down menu:Here said my own Baidu came.var tag = document.getElementById ("Myselect");alert (tag);Get the value of a labelvar opeartor = Tag.options[tag.selectedindex].value;Implementation code:DOCTYPE HTML>HTMLLang=
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:
A simple calculator is the way to achieve, you can also use JavaScri
The final effect of the following figure-2, there is a bug: Is the integer after clicking on the% result is correct, if the decimal after clicking% of the result is wrong! The other is normal, seek advice: input value is string type, in JS how to correctly handle the figure 1 in the If part??
Figure-1
Figure-2
The HTML code is as follows
The CSS code is as follows:
The JavaScript code is as follows:
Above t
The example of this article introduces JavaScript to implement a simple calculator function of the detailed code, share for everyone to reference, the specific content as follows
Effect Chart:
Implementation code:
Hopefully this article will help you learn about JavaScript programming.
Teacher decorate the task, get a day, finally put out the code, as rookie give himself a little applause, legend JS is very simple, but to write a program really need exercise, my exercise is too little, every day DotA. Hehe (made a little comment) you should be able to read it (no annotated program I don't like)Those who wish to see have some advice. In addition Eval_r to Eval, a total of 4 (do not know why this occurs after saving) A simple calcul
values during calculation.
function Value(value){ this.value = value || 0;}Value.prototype.toString = function(){ return this.value.toString(); };
Code Implementation of the createOperator function:
// IIFEvar createOperator = (function () {// name: "+", "-", "*", "/" // OPERATOR: return function (name, struct) {// closure function var Foo = function () {// obtain two operands var args = arguments; var nums = []. slice. call (arguments); nums = nums. map (function (e) {return e. value ;}); v
JS calculator code: [Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]Note:JavaScript eval () functionDefinition and usageThe eval () function computes a string and executes the JavaScript code.Return ValueReturns the string value (if any ).DescriptionThis method only accepts the original string as the parameter. If the string parameter is not the original s
123456 7 One A - $ $ - - the -Wuyi the - Wu - About $ - - - A + the - $ the the the the - in the theJquery/javascript implement a simple Web calculator
I searched the "Calculator" in JavaScript and found only three records. So I sent it out. Today it's okay to write and play. It is relatively simple. There is no technical content or practical value. It is only for beginners to learn. Experts can see if there is no problem and try again.
Test environment: IE6 + firefox1.5 passed the test
Technical description:1. documnet. getelementsbytagname usage2. bind a
(!operation) return;
Result=operatenum (result,num,operation);
Setscreen (result);
Operation=null;
Isreset=false;
}
}
}
Third:The use of global variables, the use of global variables to control the progress of local operations. (State control)
Copy Code code as follows:
var result=0;//calculation results
Whether the Var isreset=false;//is reset
var operation;//operator
Fourth:Detach and decouple the page operation.
Copy Code code as follows:
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.