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

React non-parent-child component parameter passing example code

React is a JAVASCRIPT library for building user i...

Table shows the border code you want to display

Common properties of tables The basic attributes ...

Detailed example of IOS database upgrade data migration

Detailed example of IOS database upgrade data mig...

Using jQuery to implement the carousel effect

This article shares the specific code for impleme...

CSS controls the spacing between words through the letter-spacing property

letter-spacing property : Increase or decrease th...

Vue implements 3 ways to switch tabs and switch to maintain data status

3 ways to implement tab switching in Vue 1. v-sho...

Sample code for implementing the Olympic rings with pure HTML+CSS

Rendering Code - Take the blue and yellow rings a...

Detailed explanation of the sticky position attribute in CSS

When developing mobile apps, you often encounter ...

How to use Docker-compose to deploy Django applications offline

Table of contents Install Docker-ce for the devel...

Advantages of MySQL covering indexes

A common suggestion is to create indexes for WHER...

Detailed explanation of docker nginx container startup and mounting to local

First, the structure inside the nginx container: ...

Simple CSS text animation effect

Achieve results Implementation Code html <div ...

Summary of basic usage of $ symbol in Linux

Linux version: CentOS 7 [root@azfdbdfsdf230lqdg1b...