JS Make a arithmetic calculator

Source: Internet
Author: User
Tags function calculator

Use JS to make a simple Web calculator as shown, only the simple subtraction operation

<! DOCTYPE html>
<meta charset= "UTF-8" >
<title> counters </title>
<body>
<input type= "text" name= "text" id= "pre" onblur= "validate (This.value);" >
<select id= "operator" >
<option value= "+" >+</option>
<option value= "-" >-</option>
<option value= "*" >*</option>
<option value= "/" >/</option>
</select>
<input type= "text" name= "text" id= "Next" onblur= "Validate (This.value);" >
<span>=</span>
<input type= "text" id= "result" readonly= "true" >
<input type= "button" id= "BTN" value= "Submit" onclick= "calculator ();" >
<script>
function Validate (str) {
var reg =/^\d+$/;
if (!reg.test (str)) {
Alert ("Please enter a number");
}
}
function Calculator () {
var Pre=document.getelementbyid ("Pre"). Value;
var Next=document.getelementbyid ("Next"). Value;
var Opra=document.getelementbyid ("operator"). Value;

var result=0;
Switch (OPRA) {
Case "+":
Result=parseint (PRE) +parseint (next);
Break
Case "-":
Result=parseint (PRE)-parseint (next);
Break
Case "*":
Result=parseint (PRE) *parseint (next);
Break
Case "/":
if (parseint (next)!=0) {
Result=parseint (PRE)/parseint (next);
}
else{
Alert ("Divisor cannot be 0");
Return
}
Break
Default
Break
}
document.getElementById ("Result"). Value=result;
}
</script>
</body>

JS Make a arithmetic calculator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.