Write a simple calculator using JavaScript

Write a simple calculator using JavaScript

The effect is as follows:

Reference Program:

<!DOCTYPE html>

<html lang="en">



<head>

    <meta charset="UTF-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>Analog Calculator</title>

    <style>

        button {

            width: 39px;

            height: 30px;

            background-color: rgb(102, 240, 102);

        }

        button:hover {

            background-color: black;

            color: rgb(255, 249, 237);

        }



        button:active {

            background-color: rgb(79, 72, 72);

            color: white;

        }

        table{

            background: rgb(192, 248, 109);

        }

    </style>

</head>



<body>

    <div>

        <table border="1px">

            <tr style="text-align: center;">

                <td colspan="4">

                    <input type="text" id="result" >

                </td>

            </tr>

            <tr>

                <td><button id="le" onclick="getChar('(')">(</button></td>

                <td><button id="rg" onclick="getChar(')')">)</button></td>

                <td><button id="baifen" onclick="getChar('%')">%</button></td>

                <td><button id="C" onclick="clear1()">C</button></td>

            </tr>

            <tr>

                <td><button id="seven" onclick="getChar('7')">7</button></td>

                <td><button id="eight" onclick="getChar('8')">8</button></td>

                <td><button id="nine" onclick="getChar('9')">9</button></td>

                <td><button id="divi" onclick="getChar('/')">/</button></td>

            </tr>

            <tr>

                <td><button id="four" onclick="getChar('4')">4</button></td>

                <td><button id="five" onclick="getChar('5')">5</button></td>

                <td><button id="six" onclick="getChar('6')">6</button></td>

                <td><button id="mul" onclick="getChar('*')">*</button></td>

            </tr>

            <tr>

                <td><button id="one" onclick="getChar('1')">1</button></td>

                <td><button id="two" onclick="getChar('2')">2</button></td>

                <td><button id="three" onclick="getChar('3')">3</button></td>

                <td><button id="dec" onclick="getChar('-')">-</button></td>

            </tr>

            <tr>

                <td><button id="zero" onclick="getChar('0')">0</button></td>

                <td><button id="point" onclick="getChar('.')">.</button></td>

                <td><button id="=" onclick="getResult()">=</button></td>

                <td><button id="add" onclick="getChar('+')">+</button></td>

            </tr>

        </table>

    </div>

</body>

<script>

    // Click the button to return the button value function getChar(btnid) {

        var btns = document.getElementsByTagName("button")

        switch (btnid) {

            case '+':

                setNum('+')

                break;

            case '.':

                setNum('.')

                break;

            case '=':

                setNum('=')

                break;

            case '0':

                setNum('0')

                break;

            case '1':

                setNum('1')

                break;

            case '2':

                setNum('2')

                break;

            case '3':

                setNum('3')

                break;

            case '-':

                setNum('-')

                break;

            case '4':

                setNum('4')

                break;

            case '5':

                setNum('5')

                break;

            case '6':

                setNum('6')

                break;

            case '7':

                setNum('7')

                break;

            case '8':

                setNum('8')

                break;

            case '9':

                setNum('9')

                break;

            case '/':

                setNum('/')

                break;

            case '*':

                setNum('*')

                break;

            case '(':

                setNum('(')

                break;

            case ')':

                setNum(')')

                break;

            case '%':

                setNum('%')

                break;

            default:

                break;

        }

    }

    // Clear the display function clear1() {

        document.getElementById("result").value = ""

    }

    // Display the value on the display function setNum(charCode) {

        var origin = document.getElementById('result');

        origin.value += charCode;

        origin.innerText = origin.value;

    }

    //Calculation result function getResult(){

        var res = eval(document.getElementById("result").value);

        // Append '='

        setNum('=');

        // Append result setNum(res)

    }

</script>

</html>

Note: When doing calculations, you can directly use the eval function. We don’t have to manually write the calculation logic of addition, subtraction, multiplication and division, which greatly simplifies development.

For example:

var num = eval('3+3')

The result of the operation is num=6

Eval function usage:

Syntax: eval (parameter), the parameter is a js expression, a string, which can contain variables and properties of existing objects

Return value:

Parameter Type Return Value
Integer or function Integer or function
String (and expression) The result of running the string expression
String (a statement or a statement block) Executes the statement block and returns undefined

This is the end of this article about using JavaScript to write a simple calculator. For more information about using JavaScript to write a simple calculator, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Implementing a web calculator with native JavaScript
  • Detailed explanation of the process of realizing calculator function in javascript
  • JavaScript implements simple calculator function
  • js version to realize calculator function
  • Native js to implement a simple calculator
  • Implementing a simple calculator with javascript
  • js to make a simple calculator
  • Writing a web calculator using javascript

<<:  Getting Started with Website Building for Beginners - The Conditions and Tools Needed to Build a Website

>>:  Install Kafka in Linux

Recommend

Vue Basic Tutorial: Conditional Rendering and List Rendering

Table of contents Preface 1.1 Function 1.2 How to...

Implementation of code optimization for Vue2.x project performance optimization

Table of contents 1 Use of v-if and v-show 2. Dif...

MySQL 5.7.31 64-bit free installation version tutorial diagram

1. Download Download address: https://dev.mysql.c...

Detailed tutorial of pycharm and ssh remote access server docker

Background: Some experiments need to be completed...

MySQL Quick Data Comparison Techniques

In MySQL operation and maintenance, a R&D col...

What is a MySQL index? Ask if you don't understand

Table of contents Overview From Binary Tree to B+...

Why does MySQL database index choose to use B+ tree?

Before further analyzing why MySQL database index...

Troubleshooting of master-slave delay issues when upgrading MySQL 5.6 to 5.7

Recently, when upgrading the Zabbix database from...

How to check the version of Kali Linux system

1. Check the kali linux system version Command: c...

SSM VUE Axios Detailed Explanation

Table of contents How to display SQL log? ? Descr...

canvas.toDataURL image/png error handling method recommendation

Problem background: There is a requirement to tak...

Learning Vue instructions

Table of contents 1. v-text (v-instruction name =...