js implements a simple calculator

js implements a simple calculator

Use native js to implement a simple calculator (with detailed comments) for your reference. The specific contents are as follows

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .divs {
        width: 500px;
        height: 600px;
        border: 1px solid #000000;
        margin: auto;
      }
      .divs > input {
        width: 460px;
        height: 45px;
        margin-left: 18px;
        margin-top: 10px;
        font-size: 30px;
        background-color: white;
        text-align: right;
      }
      .divs > div {
        width: 100px;
        height: 100px;
        float: left;
        border: 1px solid #000000;
        margin-left: 18px;
        margin-top: 25px;
        font-size: 40px;
        line-height: 100px;
        text-align: center;
        user-select: none;
      }
    </style>
  </head>
  <body>
    <div class="divs">
      <input type="text" value="0" id="input1" disabled />
      <div class="block">7</div>
      <div class="block">8</div>
      <div class="block">9</div>
      <div class="block">-</div>
      <div class="block">4</div>
      <div class="block">5</div>
      <div class="block">6</div>
      <div class="block">+</div>
      <div class="block">1</div>
      <div class="block">2</div>
      <div class="block">3</div>
      <div class="block">*</div>
      <div class="block">C</div>
      <div class="block">0</div>
      <div class="block">=</div>
      <div class="block">/</div>
</div>

js:

<script>
      // Get all elements with class name block var blocks = document.getElementsByClassName("block");
      // Get input
      var input = document.getElementById("input1");
      //Declare the first value var firstValue = 0,
        bool = false;
      //Declare operator var type;
      for (var i = 0; i < blocks.length; i++) {
       //Click on the i-th block
        blocks[i].onclick = function () {
          //This points to whoever is clicked. Here, this points to the element clicked each time console.log(this);
          //this.innerHTML displays the contents of the clicked div (such as 1,2,3,-,+)
          //Judge whether the clicked value is a number (either NaN or a number)
          if (!isNaN(this.innerHTML)) {      
            // bool is initially false. When bool is false, you can continue to input. When bool is true, the input is cleared to 0
            if (bool) {
              input.value = "0";
              bool = false;
            }
            // Add the value in input to the content clicked, and convert it to a number to remove the leading 0, and then convert it to a character input.value = Number(input.value + this.innerHTML).toString();
          } else {
           //Judge whether the click is + - * /if (this.innerHTML !== "C" && this.innerHTML !== "=") {
              //Save the first number to firstValue 
              firstValue = Number(input.value);
              //Store the operator in type
              type = this.innerHTML;
              //Reset the value in input to 0
              input.value = "0";
            } else if (this.innerHTML === "C") { //Judge the situation of clicking C//Reset all firstValue = 0;
              type = undefined;
              input.value = "0";
            } else { //Judge the situation of clicking = //Perform operations according to the type of operator switch (type) {
                case "+":
                  input.value = (firstValue + Number(input.value)).toString();
                  break;
                case "-":
                  input.value = (firstValue - Number(input.value)).toString();
                  break;
                case "*":
                  input.value = (firstValue * Number(input.value)).toString();
                  break;
                case "/":
                  // Reset input.value when the divisor is 0
                  if (Number(input.value) === 0) input.value = "0";
                  else
                    input.value = (firstValue / Number(input.value)).toString();
                  break;
              }
              //When bool is true, after clicking "=", when you enter again, input.value is 0
              bool = true;
            }
          }
        };
      }

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:
  • js accurate countdown function sharing
  • Super accurate javascript method to verify ID number
  • js drag and drop table to realize content calculation
  • JS implementation of Apple calculator
  • JavaScript simulation calculator
  • JavaScript to achieve simple calculator function
  • How to calculate the number of lines of text in JavaScript
  • JavaScript classic case simple calculator
  • js precise calculation

<<:  Explanation of monitoring parameters in performance and sys schema in MySQL 5.7 (recommended)

>>:  Summary of solutions to common Linux problems

Recommend

Use Docker to run multiple PHP versions on the server

PHP7 has been out for quite some time, and it is ...

Vue project implements file download progress bar function

There are two common ways to download files in da...

Six inheritance methods in JS and their advantages and disadvantages

Table of contents Preface Prototype chain inherit...

Several methods to solve the problem of MySQL fuzzy query index failure

When we use the like % wildcard, we often encount...

Advantages and Problems of XHTML CSS Website Design

XHTML is the standard website design language cur...

Detailed explanation of vite2.0 configuration learning (typescript version)

introduce You Yuxi’s original words. vite is simi...

HTML5+CSS3 coding standards

The Golden Rule No matter how many people are wor...

MySQL master-slave replication principle and points to note

Written in front I have been writing a special to...

js Promise concurrent control method

Table of contents question background Idea & ...

How to configure the My.ini file when installing MySQL5.6.17 database

I recently used the MySql database when developin...

Detailed explanation of built-in methods of javascript array

Table of contents 1. Array.at() 2. Array.copyWith...

HTML+CSS+JS to implement the Don't Step on the Whiteboard game

Table of contents Background 1. Thought Analysis ...

Detailed explanation of this pointing problem in JavaScript

Preface Believe me, as long as you remember the 7...

Apache ab concurrent load stress test implementation method

ab command principle Apache's ab command simu...

MySQL users and permissions and examples of how to crack the root password

MySQL Users and Privileges In MySQL, there is a d...