Implementing a simple calculator based on JavaScript

Implementing a simple calculator based on JavaScript

This article shares the specific code of JavaScript to implement a simple calculator for your reference. The specific content is as follows

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Calculator</title>
    <style>
        .op {
            margin: 50px auto;
            width: 300px;
            height: 300px;
            border: 1px solid orange;
            background-color: lightskyblue;
            border-radius: 50px;
        }

        input {
            width: 210px;
            height: 30px;
            border-radius: 5px;
        }

        button {
            width: 30px;
            height: 30px;
            border-radius: 10px;
        }
    </style>
</head>
<body>
<div class="op">
    <h2 align="center">Calculator</h2>
    <form name="calculator">
        <table align="center">
            <tr>
                <td>num1:</td>
                <td><input type="text" name="num1"></td>
            </tr>
            <tr>
                <td>num2:</td>
                <td><input type="text" name="num2"></td>
            </tr>
            <tr>
                <td colspan="2">&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                    <button type="button">+</button>
                    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                    <button type="button">-</button>
                    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                    <button type="button">*</button>
                    &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
                    <button type="button">/</button>
                </td>
            </tr>
            <tr>
                <td>Results:</td>
                <td><input type="text" name="result" disabled></td>
            </tr>
        </table>
    </form>
</div>
<script>
    //Get all element objects under the tag name and return an array let buttonArr = document.getElementsByTagName("button");
    for (let but of buttonArr) {
        but.addEventListener('click', function () {
            let operator = this.innerHTML;
            count(operator);
        });
    }

    function count(operator) {
        //Get the input value of the form calculator named num1 let num1 = parseFloat(document.calculator.num1.value);
        let num2 = parseFloat(document.calculator.num2.value);
        let result = '';
        switch (operator) {
            case '+':
                result = num1 + num2;
                break;
            case '-':
                result = num1 - num2;
                break;
            case '*':
                result = num1 * num2;
                break;
            case '/':
                if (num2 == 0) {
                    alert("The divisor cannot be zero");
                    return;
                }
                result = num1 / num2;
                break;
        }
//Assign the result to the input box with the attribute name result document.calculator.result.value = result;
    }
</script>
</body>
</html>

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

<<:  Explanation of the configuration and use of MySQL storage engine InnoDB

>>:  A brief understanding of several scheduling algorithms for Nginx seven-layer load balancing

Recommend

Share some uncommon but useful JS techniques

Preface Programming languages ​​usually contain v...

Common functions of MySQL basics

Table of contents 1. Common function classificati...

Tutorial on using Docker Compose to build Confluence

This article uses the "Attribution 4.0 Inter...

CSS3 timeline animation

Achieve results html <h2>CSS3 Timeline</...

Summary of javascript date tools

let Utils = { /** * Is it the year of death? * @r...

How to use DCL to manage users and control permissions in MySQL

DCL (Data Control Language): Data control languag...

Summary of Ubuntu backup methods (four types)

Method 1: To use respin, follow these steps: sudo...

Vue uses the Element el-upload component to step on the pit

Table of contents 1. Basic Use 2. Image quantity ...

Detailed Analysis of or, in, union and Index Optimization in MySQL

This article originated from the homework assignm...

10 SQL statement optimization techniques to improve MYSQL query efficiency

The execution efficiency of MySQL database has a ...

The difference between html empty link href="#" and href="javascript:void(0)"

# contains a location information. The default anc...

Vue custom directive details

Table of contents 1. Background 2. Local custom i...

Mini Program to implement Token generation and verification

Table of contents process Demo Mini Program Backe...