A simple jQuery calculator for continuous computation

  • 2020-03-30 03:33:00
  • OfStack

A simple jQuery calculator, just to achieve a continuous calculation function


<!DOCTYPE html> 
<html> 
<head> 
<meta charset="utf8"/> 
<title>Javascript The calculator </title> 
</head> 
<body> 
<table> 
<tr> 
<td colspan="4"><input id="show" value="0"/></td> 
</tr> 
<tr> 
<td><button onclick="number(this)" value="7">7</button></td> 
<td><button onclick="number(this)" value="8">8</button></td> 
<td><button onclick="number(this)" value="9">9</button></td> 
<td><button onclick="calsym(this)" value="+">+</button></td> 
</tr> 
<tr> 
<td><button onclick="number(this)" value="4">4</button></td> 
<td><button onclick="number(this)" value="5">5</button></td> 
<td><button onclick="number(this)" value="6">6</button></td> 
<td><button onclick="calsym(this)" value="-">-</button></td> 
</tr> 
<tr> 
<td><button onclick="number(this)" value="1">1</button></td> 
<td><button onclick="number(this)" value="2">2</button></td> 
<td><button onclick="number(this)" value="3">3</button></td> 
<td><button onclick="calsym(this)" value="*">*</button></td> 
</tr> 
<tr> 
<td><button onclick="number(this)" value="0">0</button></td> 
<td><button onclick="calsym(this)" value="=">=</button></td> 
<td><button onclick="clearCal()" value="c">c</button></td> 
<td><button onclick="calsym(this)" value="/">/</button></td> 
</tr> 
</table> 
</body> 
<script type="text/javascript" src="jquery-1.9.1.js"></script> 
<script type="text/javascript" src="jquery-1.9.1.min.js"></script> 
<script type="text/javascript"> 
var num0 = null; 
var num1 = null; 
var symble = null; 
var lastclick = null; 
var symarr = new Array(); //Symbol set
symarr[0] = '+'; 
symarr[1] = '/'; 
symarr[2] = '*'; 
symarr[3] = '-'; 
symarr[4] = '='; 
function number(n){ 
var numnow = $('#show'); 
var _exist=$.inArray(lastclick,symarr); //Determine whether the last click is a symbol
if (numnow.val() == 0 || _exist != -1) {//If the display box is 0 or the last click is a symbol, then record the display box again
numnow.val($(n).val()); 
}else{ 
numnow.val(parseInt(numnow.val()) * 10 +parseInt($(n).val())); 
} 
lastclick = $(n).val(); //Update last click
} 

function calsym(cs){ //Symbol clicks on the event response
var numnow = $('#show'); 
var _exist=$.inArray(lastclick,symarr); 
if (num0 == null && symble == null) { //The initial state
num0 = numnow.val(); 
symble = $(cs).val(); 
}else if(num0 != null && num1 == null && _exist !=-1){ //Processing of the continuous point operator and the first click operator
symble = $(cs).val(); 
}else{ //Normal computing state
num1 = numnow.val(); 
var result = calculate(symble,num0,num1); 
symble = $(cs).val(); //Uplink and line: calculate the previous operator and the result, and then update the operator to this click
num0 = result; 
numnow.val(result); 
num1 = null; 
} 
lastclick = $(cs).val(); //Update last click
} 

function clearCal(){ //Empty function
$('#show').val('0'); 
num0 = null; 
num1 = null; 
symble = null; 
lastclick = null; 
} 

function calculate(sym,m,n){ //Calculate and return the result
var res = null; 
m = parseInt(m); 
n = parseInt(n); 
switch(sym){ 
case '+': 
res = m+n; 
break; 
case '-': 
res = m-n; 
break; 
case '*': 
res = m*n; 
break; 
case '/': 
if (n == 0) { 
alert("false"); 
break; 
} 
res = m/n; 
break; 
default: 
break; 
} 
return res; 
} 
</script> 
</html>

Related articles: