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