Implementing a simple calculator with javascript

Implementing a simple calculator with javascript

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

Design a simple calculator

Code

 <body>
  <a>First number</a>
  <input type="test" id="inputId1" value="" /><br/>
  <a>Second number</a>
  <input type="test" id="inputId2" value="" /><br/>
  <button onclick="cal('+')">+</button>
  <button onclick="cal('-')">-</button>
  <button onclick="cal('*')">*</button>
  <button onclick="cal('/')">/</button><br/>
  Calculation results
  <input type="test" id="resultId" value="" />
  <script type="text/javascript">
   // function add() {
   // console.log('add');
   // var inputObj1 = document.getElementById('inputId1');
   // var inputObj2 = document.getElementById('inputId2');
   // var result = parseInt(inputObj1.value) + parseInt(inputObj2.value);
   // var resultObj = document.getElementById('result');
   // resultObj.value = result;
   // console.log(result);
   // }
   function cal(type) {
    var inputObj1 = document.getElementById('inputId1');
    var inputObj2 = document.getElementById('inputId2');
    switch(type){
     case '+':
      var result = parseInt(inputObj1.value) + parseInt(inputObj2.value);
      break;
     case '-':
      var result = parseInt(inputObj1.value) - parseInt(inputObj2.value);
      break;
     case '*':
      var result = parseInt(inputObj1.value) * parseInt(inputObj2.value);
      break;
     case '/':
      var result = parseInt(inputObj1.value) / parseInt(inputObj2.value);
      break;
    }
    var resultObj = document.getElementById('resultId');
    resultObj.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:
  • Implementing a simple age calculator based on HTML+JS
  • 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
  • Writing a web calculator using javascript
  • JavaScript Example - Implementing a Calculator

<<:  Summary of basic SQL statements in MySQL database

>>:  How to run Python script on Docker

Recommend

4 solutions to mysql import csv errors

This is to commemorate the 4 pitfalls I stepped o...

Linux process management tool supervisor installation and configuration tutorial

Environment: CentOS 7 Official documentation: htt...

Implementing custom radio and check box functions with pure CSS

1. Achieve the effect 2 Knowledge Points 2.1 <...

Calculation of percentage value when the css position property is absolute

When position is absolute, the percentage of its ...

Examples of using && and || operators in javascript

Table of contents Preface && Operator || ...

Detailed explanation of rpm installation in mysql

View installation and uninstallation # View rpm -...

MySQL Series 7 MySQL Storage Engine

1. MyISAM storage engine shortcoming: No support ...

MySQL series 6 users and authorization

Table of contents Tutorial Series 1. User Managem...

Difference and implementation of JavaScript anti-shake and throttling

Table of contents 1. Anti-shake 2. Throttling 3. ...

JavaScript Basics Variables

Table of contents 1. Variable Overview 1.1 Storag...

Application of anchor points in HTML

Set Anchor Point <a name="top"><...

How to quickly modify the root password under CentOS8

Start the centos8 virtual machine and press the u...

How to solve the mysql error 1033 Incorrect information in file: 'xxx.frm'

Problem Description 1. Database of the collection...

Tutorial on installing and configuring remote login to MySQL under Ubuntu

This article shares the MySQL installation and co...