


Javascript example of how to implement the most basic arithmetic operator
This article mainly introduces in detail the code related to implementing the basic arithmetic operator Javascript, which has certain reference value. Interested friends can refer to it
Using Javascript Implement a basic calculator, the specific content is as follows
Use table layout, JS to add events
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>计算器</title> </head> <style> *{ margin: 0; padding: 0; } table{ width:400px; height: 600px; border-collapse: collapse; margin: auto; } .trr{ width: 400px; height: 100px; } .trr1{ width: 400px; height: 50px; } .tdd{ width: 100px; height: 100px; border: 1px solid silver; text-align: center; line-height: 100px; } .btn{ width: 100%; height: 100%; font-size: 2.5em; } .btn1{ width: 100%; height: 100%; font-size: 2.5em; } .tdd1{ width: 100px; height: 50px; } .text{ height: 100%; font-size: 2.5em; text-align: right; } </style> <body> <table> <tr class="trr text1"> <td class="tdd" colspan="4"><input class="text" type="text" disabled value="0" /></td> </tr> <tr class="trr1"> <td class="tdd1" colspan="2"><input class="btn1 btn" type="button" value="c"/></td> <td class="tdd1" colspan="2"><input class="btn1 btn" type="button" value="d"/></td> </tr> <tr class="trr"> <td class="tdd"><input class="btn" type="button" value="7"/></td> <td class="tdd"><input class="btn" type="button" value="8"/></td> <td class="tdd"><input class="btn" type="button" value="9"/></td> <td class="tdd"><input class="btn" type="button" value="/"/></td> </tr> <tr class="trr"> <td class="tdd"><input class="btn" type="button" value="4"/></td> <td class="tdd"><input class="btn" type="button" value="5"/></td> <td class="tdd"><input class="btn" type="button" value="6"/></td> <td class="tdd"><input class="btn" type="button" value="*"/></td> </tr> <tr class="trr"> <td class="tdd"><input class="btn" type="button" value="1"/></td> <td class="tdd"><input class="btn" type="button" value="2"/></td> <td class="tdd"><input class="btn" type="button" value="3"/></td> <td class="tdd"><input class="btn" type="button" value="-"/></td> </tr> <tr class="trr"> <td class="tdd"><input class="btn" type="button" value="0"/></td> <td class="tdd"><input class="btn" type="button" value="."/></td> <td class="tdd"><input class="btn" type="button" value="+"/></td> <td class="tdd"><input class="btn" type="button" value="="/></td> </tr> </table> <script> var obtn=document.getElementsByClassName("btn"); var otext=document.getElementsByClassName("text")[0]; var arr=[];//定义一个数组,向其中存入数字和运算符。 for(var i=0;i<obtn.length;i++){ obtn[i].onclick= function () { if(!isNaN(this.value)||this.value=="."){ //this:代表鼠标点击的obtn if(otext.value.indexOf(".")==-1){ //消除重复"."的BUG if(otext.value.length==0){ if(this.value!="0"){ //---------------------- otext.value+=this.value; //| } //| } //| else if(otext.value.length==1&&otext.value=="0"){//| otext.value=this.value; //| } //| else if(otext.value.length==1&&otext.value!="0"){//初始状态时,若计算器屏幕为"0", otext.value+=this.value; //实现输入一个非零数字的时候,计算器 } //上的数值替换为输入的非零值 else if(otext.value.length>1){ //| otext.value+=this.value; //| } //-------------------- } else { if(this.value!="."){ //消除重复"."的BUG if(otext.value.length==0){ if(obtn[i].value!="0"){ otext.value+=this.value; } } if(otext.value.length>=1){ otext.value+=this.value; } } } } if(this.value=="/"||this.value=="*"||this.value=="+"||this.value=="-"){ if(otext.value!="0"&&otext.value!=""&&otext.value!="-"){ //消除输入重复运算符的BUG, arr[arr.length]=otext.value; //当输入一个运算符的时候,otext内的value值 arr[arr.length]=this.value; //为""(空),所以判断条件为若otext内的value值不为空 otext.value=""; //则向数字中传值。 } //此时出现无法输入负数值运算的BUG else if(otext.value==""&&this.value=="-"){ //消除无法输入负数值运算的BUG otext.value=this.value; //当点击运算符后otext的value值为空, } //此时判断若this的值为"-",就替换进去。 else if(otext.value=="0"&&this.value=="-"){//此时出现无法执行类似"3--3"的双减法运算BUG, otext.value=this.value; //因为eval()无法识别有双减的字符串值。 } //若初始时,otext值为"0",并且this的值为 } //"-",则用"-"替换otext中的值。 if(this.value=="="){ if(otext.value.length>=1){ //-------------------- var string=""; //| if(arr[arr.length-1]=="-"&&otext.value<0){ //→消除无法执行类似"3--3"的双减法运算的BUG arr[arr.length-1]="+"; //→当输入负数值的时候,判断arr数组中的 otext.value=Math.abs(otext.value); //→末尾值是否为"-",若为"-"则把其改为"+", } //→并且让otext.value值取绝对值。 arr[arr.length]=otext.value; //| for(var i=0;i<arr.length;i++){ //| string+=arr[i]; //| } //|把存入数组中的数字和运算符遍历存储到一个字符串中, otext.value=eval(string); //|直接使用eval()方法就可以识别一个的字符串,执行 arr=[]; //|该字符串中的运算 } //| } //--------------------- if(this.value=="c"){ otext.value="0"; arr=[]; } if(this.value=="d"){ otext.value=otext.value.substr(0,otext.value.length-1);//每一次删除otext中的末尾值 if(otext.value==""){ //当把otext中的值删除完后,给 otext.value="0"; //otext复值"0". } } } } </script> </body> </html>
The "c" function of the calculator is to clear the screen; The "d" function is to delete a number;
The above is the detailed content of Javascript example of how to implement the most basic arithmetic operator. For more information, please follow other related articles on the PHP Chinese website!

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.

C and C play a vital role in the JavaScript engine, mainly used to implement interpreters and JIT compilers. 1) C is used to parse JavaScript source code and generate an abstract syntax tree. 2) C is responsible for generating and executing bytecode. 3) C implements the JIT compiler, optimizes and compiles hot-spot code at runtime, and significantly improves the execution efficiency of JavaScript.

JavaScript's application in the real world includes front-end and back-end development. 1) Display front-end applications by building a TODO list application, involving DOM operations and event processing. 2) Build RESTfulAPI through Node.js and Express to demonstrate back-end applications.

The main uses of JavaScript in web development include client interaction, form verification and asynchronous communication. 1) Dynamic content update and user interaction through DOM operations; 2) Client verification is carried out before the user submits data to improve the user experience; 3) Refreshless communication with the server is achieved through AJAX technology.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 Chinese version
Chinese version, very easy to use