JavaScript implements simple calculator function

JavaScript implements simple calculator function

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

The specific requirements are as follows:

Implementation code:

<html>
  <head>
    <meta charset="utf-8">
    <title>Calculator</title>
    <script>
      function myck(type){
        var num1 = document.getElementById("num1");
        var num2 = document.getElementById("num2");
        if(type==1){
          // Calculation operation var result = parseInt(num1.value) + parseInt(num2.value);
          alert(result);
          document.getElementById("resultDiv").innerText = "Final calculation result: " + result;
        }else if(type==2){
    var result = parseInt(num1.value) - parseInt(num2.value);
             alert(result);
             document.getElementById("resultDiv").innerText = "Final calculation result: " + result;
   }
     else if(type==3){
     var result = parseInt(num1.value) * parseInt(num2.value);
     alert(result);
     document.getElementById("resultDiv").innerText = "Final calculation result: " + result;
      }
         else if(type==4){
        if(confirm("Is it cleared correctly?")){
        // Clear num1.value = "";
        num2.value = "";
        document.getElementById("resultDiv").innerText="";
      }
           }
      }
    </script>
  </head>
  <body>
    <div style="margin-top: 100px;margin-left: 500px;">
      <span style="font-size: 60px;">Calculator</span>   
    </div>
    <div>
      <div class="innerDiv" style="margin-left: 490px;">
        Number 1: <input id="num1" type="number" placeholder="Please enter number 1"> 
      </div>
    </div>
    <div>
      <div class="innerDiv" style="margin-left:490px;">
      Number 2: <input id="num2" type="number" placeholder="Please enter number 2">
     </div> 
    </div>
    <div>
      <div style="margin-left: 500px;" class="innerDiv">
        <input type="button" onclick="myck(1)" value="Add">
    <input type="button" onclick="myck(2)" value="Subtract">
    <input type="button" onclick="myck(3)" value="Multiply">
        <input type="button" onclick="myck(4)" value="Clear&nbsp;&nbsp;Empty">
      </div> 
    </div>
    <div id="resultDiv">

    </div>
  </body>
  <style>
    .innerDiv{
      margin-left: 420px;
      margin-top: 20px;
    }
  </style>
</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
  • javascript-simple calculator implementation step decomposition (with pictures)
  • Pure javascript code to implement calculator functions (three methods)
  • js implements a simple calculator
  • Web calculator A JS calculator
  • Simple implementation of js web calculator

<<:  How to use nginx to simulate blue-green deployment

>>:  How to solve the problem that MySQL cannot start because it cannot create PID

Recommend

Tomcat parses XML and creates objects through reflection

The following example code introduces the princip...

Implementation of multiple instances of tomcat on a single machine

1. Introduction First of all, we need to answer a...

JavaScript anti-shake and throttling explained

Table of contents Stabilization Throttling Summar...

Detailed explanation of the installation steps of the MySQL decompressed version

1. Go to the official website: D:\mysql-5.7.21-wi...

IDEA complete code to connect to MySQL database and perform query operations

1. Write a Mysql link setting page first package ...

Detailed explanation of the use of Vue.js render function

Vue recommends using templates to create your HTM...

Summary of 3 minor errors encountered during MySQL 8.0 installation

Preface In the past, the company used the 5.7 ser...

Mysql tree-structured database table design

Table of contents Preface 1. Basic Data 2. Inheri...

Detailed explanation of Docker usage under CentOS8

1. Installation of Docker under CentOS8 curl http...

What are the image file formats and how to choose

1. Which three formats? They are: gif, jpg, and pn...

React + Threejs + Swiper complete code to achieve panoramic effect

Let’s take a look at the panoramic view effect: D...

How to build mysql master-slave server on centos7 (graphic tutorial)

This article mainly introduces how to build a MyS...

Vertical and horizontal splitting of MySQL tables

Vertical Split Vertical splitting refers to the s...

How to disable IE10's password clear text display and quick clear function

IE10 provides a quick clear button (X icon) and a ...