Using JS to implement a simple calculator

Using JS to implement a simple calculator

Use JS to complete a simple calculator for your reference. The specific contents are as follows

Requirements: The input value can only be a number, use a regular expression
onchange(): Executes an event when the value changes
onblur(): Executes an event when the mouse moves out

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Calculator</title>
</head>
<body>
    <div style="text-align: center;">
        <input type="text" id="1" onkeyup="this.value=this.value.replace(/[^\d]/g,'') " >
    <select name="select" id="select">
        <option value="null">Please select</option>
        <option value="0">+</option>
        <option value="1">-</option>
        <option value="2">*</option>
        <option value="3">/</option>
    </select>
    <input type="text" id="2" onkeyup="this.value=this.value.replace(/[^\d]/g,'') ">
    =
    <input type="text" id="3" onkeyup="this.value=this.value.replace(/[^\d]/g,'') "><br><br>
    </div>
    <div style="text-align: center;">
        <input type="button" id="button"value="calculate">
    </div>
    <script>
        
    function accAdd(arg1,arg2){ 
        var r1,r2,m; 
        try{
            r1=arg1.toString().split(".")[1].length
        }catch(e){
            r1=0
        } 
        try{
            r2=arg2.toString().split(".")[1].length
        }catch(e){
            r2=0
        } 
        m=Math.pow(10,Math.max(r1,r2)) 
        return (arg1*m+arg2*m)/m 
        } 
        Number.prototype.add = function (arg) { 
        return accAdd(arg,this); 
        }


    function Subtr(arg1,arg2){
        var r1,r2,m,n;
 
        try{
            r1=arg1.toString().split(".")[1].length
        }catch(e){
            r1=0
        }
 
        try{
            r2=arg2.toString().split(".")[1].length
        }catch(e){
            r2=0
        }
        m=Math.pow(10,Math.max(r1,r2));
 
     // last modified by deeka
     // Dynamically control the precision length n=(r1>=r2)?r1:r2;
        return ((arg1*m-arg2*m)/m).toFixed(n);
    }
    function accMul(arg1,arg2) //multiplication { 
        var m=0,s1=arg1.toString(),s2=arg2.toString(); 
        try{
            m+=s1.split(".")[1].length
        }catch(e){} 
        try{
            m+=s2.split(".")[1].length
        }catch(e){} 
        return Number(s1.replace(".",""))*Number(s2.replace(".","")) / Math.pow(10,m) 
        } 
 
    //Add a mul method to the Number type to make it more convenient to call. 
    Number.prototype.mul = function (arg) { 
    return accMul(arg, this); 
    } 

    function accDiv(arg1,arg2){ 
    var t1=0,t2=0,r1,r2; 
    try{
        t1=arg1.toString().split(".")[1].length
    }catch(e){} 
    try{
        t2=arg2.toString().split(".")[1].length
    }catch(e){} 
     with(Math){ 
        r1=Number(arg1.toString().replace(".","")) 
        r2=Number(arg2.toString().replace(".","")) 
        return (r1/r2)*pow(10,t2-t1); 
        } 
    } 
        Number.prototype.div = function (arg) { 
        return accDiv(this, arg); 
    }


        document.getElementById('button').onclick=function(){
            var num1 = document.getElementById('1').value;
            var num2 = document.getElementById('2').value;
            var num3;
            var op = document.getElementById('select').value;
            switch(op){
                case '0':
                    num3 = accAdd(num1,num2);
                    break;
                case '1':
                    num3 = Subtr(num1,num2);
                    break;
                case '2':
                    num3=accMul(num1,num2);
                    break;
                case '3':
                    num3=accDiv(num1,num2);
                    break;
            }
            document.getElementById('3').value=num3;
        }
       
    </script>

</body>
</html>

Effect picture:

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • A simple calculator written in javascript, with a lot of content and practical methods. Recommended
  • Simple js code to realize calculator operation
  • js implements a simple calculator
  • HTML+JS implements simple calculator code (addition, subtraction, multiplication and division)
  • Simple calculator implementation code written in JS
  • js implements a simple calculator
  • javascript-simple calculator implementation step decomposition (with pictures)
  • Pure javascript code to implement calculator functions (three methods)
  • Simple implementation of js web calculator
  • Web calculator A JS calculator

<<:  Summary of MySQL common SQL statements including complex SQL queries

>>:  Zabbix monitors the process of Linux system services

Recommend

Linux Domain Name Service DNS Configuration Method

What is DNS The full name of DNS is Domain Name S...

Summary of MySQL date and time functions (MySQL 5.X)

1. MySQL gets the current date and time function ...

How to deploy MySQL and Redis services using Docker

Table of contents How to deploy MySQL service usi...

How to view the database installation path in MySQL

We can view the installation path of mysql throug...

Automatic failover of slave nodes in replication architecture in MySQL 8.0.23

I have been in contact with MGR for some time. Wi...

Zen HTML Elements Friends who use zen coding can collect it

html ¶ <html></html> html:xml ¶ <h...

How to dynamically modify container port mapping in Docker

Preface: Docker port mapping is often done by map...

Mysql8.0 uses window functions to solve sorting problems

Introduction to MySQL Window Functions MySQL has ...

MySQL independent index and joint index selection

There is often a lack of understanding of multi-c...

How to track users with JS

Table of contents 1. Synchronous AJAX 2. Asynchro...

Detailed explanation of MySQL database binlog cleanup command

Overview Today I will mainly share how to correct...

What is flex and a detailed tutorial on flex layout syntax

Flex Layout Flex is the abbreviation of Flexible ...

Interaction in web design: A brief discussion on paging issues

Function: Jump to the previous page or the next p...

WeChat applet realizes horizontal and vertical scrolling

This article example shares the specific code for...

Native js to implement 2048 game

2048 mini game, for your reference, the specific ...