Implementing a web calculator based on JavaScript

Implementing a web calculator based on JavaScript

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

First look at the effect:

In addition, the calculator also comes with digital and operator checking functions:

Paste the source code below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Calculator</title>
<script language="javascript">

 var oper1=prompt("Please enter the first operand:");
 var oper2 = prompt("Please enter the second operand: ");
 var operator=prompt("Please enter the operator (+, -, *, /)");
 parse();
 var result;
 switch(operator)
 {
  case"+":
   result=doSum(oper1,oper2);
   alert(oper1+"+"+oper2+"="+result);
   break;
  case"-":
   result = doSubstract (oper1, oper2);
   alert(oper1+"-"+oper2+"="+result);
   break;
  case"*":
   result = doMultiply(oper1,oper2);
   alert(oper1+"*"+oper2+"="+result);
   break;
  case"/":
   result = doDivide(oper1,oper2);
   alert(oper1+"/"+oper2+"="+result);
   break;
  default:
   alert("The operator entered is illegal");
  }
  function parse(){
   if(isNaN(oper1)||isNaN(oper2)){
    alert("The number entered is illegal");
    }
   else{
    oper1 = parseFloat(oper1);
    oper2 = parseFloat(oper2);
    }
   }
  function doSum(oper1,oper2){
   return oper1+oper2;
   }
  function doSubstract(oper1,oper2){
   return oper1-oper2;
   }
  function doMultiply(oper1,oper2){
   return oper1*oper2;
   }
  function doDivide(oper1,oper2){
   return oper1/oper2;
   }
</script>
</head>

<body>
</body>
</html>

Of course, only the JavaScript source code is shown here. You can also use html+css to design a nice appearance for the calculator. You can feel free to use it.

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)
  • js implements a simple calculator
  • Pure javascript code to implement calculator functions (three methods)
  • Simple implementation of js web calculator
  • Web calculator A JS calculator

<<:  Detailed explanation of common MySQL operation commands in Linux terminal

>>:  Complete steps for mounting a new data disk in CentOS7

Blog    

Recommend

JavaScript to achieve full or reverse selection effect in form

This article shares the specific code of JavaScri...

An IE crash bug

Copy code The code is as follows: <style type=...

How to view the IP address of Linux in VMware virtual machine

1. First, double-click the vmware icon on the com...

VMware Workstation 15 Pro Installation Guide (for Beginners)

01. VMware Workstation Pro 15 Download Download: ...

Native js to achieve seamless carousel effect

Native js realizes the carousel effect (seamless ...

uniapp project optimization methods and suggestions

Table of contents 1. Encapsulate complex page dat...

Tutorial on installing mysql5.7.18 on mac os10.12

I searched the entire web and found all kinds of ...

Detailed explanation of how to view the current number of MySQL connections

1. View the detailed information of all current c...

HTML+CSS3 code to realize the animation effect of the solar system planets

Make an animation of the eight planets in the sol...

Example of cross-database query in MySQL

Preface In MySQL, cross-database queries are main...

Implementation steps for setting up the React+Ant Design development environment

Basics 1. Use scaffolding to create a project and...

How to use echarts to visualize components in Vue

echarts component official website address: https...

Detailed explanation of Bootstrap grid vertical and horizontal alignment

Table of contents 1. Bootstrap Grid Layout 2. Ver...

JS uses clip-path to implement dynamic area clipping function

background Today, I was browsing CodePen and saw ...