How to implement a calculator using JavaScript
This article mainly introduces the ultra-simple calculator function implemented in JavaScript, which can realize the basic four arithmetic operations and has verification functions. The code is equipped with more detailed comments for easy understanding. Friends in need can refer to it
The example in this article describes the ultra-simple calculator function implemented in JavaScript. Share it with everyone for your reference, the details are as follows:
Let’s take a look at the running effect first:
The specific code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>www.jb51.net JS计算器</title> <script type="text/javascript"> // window.onload 获取元素getElementById window.onload = function(){ var oTxt1 = document.getElementById('val01'); var oTxt2 = document.getElementById('val02'); var oFuhao = document.getElementById('fuhao'); // 这三个要放在button函数里面,因为s1.value是获取input里面的输入,但是这个时候还没有输入了 // var iNum1 = oTxt1.value; // var iNum2 = oTxt2.value; // var iNum3 = oFuhao.value; oBtn = document.getElementById('btn'); // 计算按钮点击事件 oBtn.onclick = function(){ var iNum1 = oTxt1.value; var iNum2 = oTxt2.value; var iNum3 = oFuhao.value; var iResult; //如果两个输入有一个是空的话 //return是让if里面执行结束 if (iNum1=='' || iNum2=='') { alert('不能为空'); return; } //isNaN() 如果是true,说明是非数字,所以如果两个输入中有非数字,就提示alert if (isNaN(iNum1) || isNaN(iNum2)) { alert('不能有字母'); return; } //对+-*/四个操作对应的value进行判断 //如果直接iNum1+iNum2 输出的结果是字符串的拼接 12+24 1224 所以要转换成parseInt整数 if (iNum3 == 0) { iResult = parseInt(iNum1) + parseInt(iNum2) } else if (iNum3 == 1) { iResult = parseInt(iNum1) - parseInt(iNum2) } else if (iNum3 == 2) { iResult = parseInt(iNum1) * parseInt(iNum2) } else if (iNum3 == 3) { iResult = parseInt(iNum1)/parseInt(iNum2) } alert(iResult); } } </script> </head> <body> <h3>计算器</h3> <input type="text" id="val01"> <select id="fuhao"> <option value="0">+</option> <option value="1">-</option> <option value="2">*</option> <option value="3">/</option> </select> <input type="text" id="val02"> <input type="button" id="btn" value="计算"> </body> </html>
That’s it I compiled the text, I hope it will be helpful to everyone
Related articles:
How to generate a random scrambled array in JS
In Common components and framework structures in vue (detailed tutorial)
How to implement animated check boxes in anime.js
The above is the detailed content of How to implement a calculator using JavaScript. For more information, please follow other related articles on the PHP Chinese website!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Efficient Fibonacci sequence calculator: PHP implementation of Fibonacci sequence is a very classic mathematical problem. The rule is that each number is equal to the sum of the previous two numbers, that is, F(n)=F(n -1)+F(n-2), where F(0)=0 and F(1)=1. When calculating the Fibonacci sequence, it can be implemented recursively, but performance problems will occur as the value increases. Therefore, this article will introduce how to write an efficient Fibonacci using PHP

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).
