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

Text pop-up effects implemented with CSS3

Achieve resultsImplementation Code html <div&g...

Detailed steps for installing Tomcat, MySQL and Redis with Docker

Table of contents Install Tomcat with Docker Use ...

How to solve the problem of margin overlap

1. First, you need to know what will trigger the v...

Implementation steps for installing java environment in docker

This article is based on Linux centos8 to install...

js to achieve simple magnifying glass effects

This article example shares the specific code of ...

A Brief Analysis of Patroni in Docker Containers

Table of contents Create an image File Structure ...

Implementation of Nginx filtering access logs of static resource files

Messy log Nginx in daily use is mostly used as bo...

Detailed explanation of the solution for migrating antd+react projects to vite

Antd+react+webpack is often the standard combinat...

Understand the principles and applications of JSONP in one article

Table of contents What is JSONP JSONP Principle J...

How to ensure the overall user experience

Related Articles: Website Design for User Experien...

Detailed explanation of the use of Join in Mysql

In the previous chapters, we have learned how to ...

The practical process of login status management in the vuex project

Table of contents tool: Login scenario: practice:...