-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc.js
More file actions
49 lines (38 loc) · 1.04 KB
/
calc.js
File metadata and controls
49 lines (38 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var symbols = ["/","*","-","+","."]
$( document ).ready(function() {
var display = "0";
$("#display").html(display);
$('button').click(getValue)
function getValue(){
var currVal = $(this).val()
var functional = true;
if (symbols.includes(currVal) ){
functional = handleOp(currVal,display);
}
if (functional){
if ($(this).val()=='equals'){
display = eval(display);
}
else if($(this).val()=='clear'){
display = "0";
}
else if (display==='0'){
display = $(this).val();
}
else{
display += $(this).val();
}
if (display.length>10){
display = display.substr(0,11)
}
}
$("#display").html(display);
}
function handleOp(op,x){
var lastChar = x[x.length-1];
if (symbols.includes(lastChar)){
return false;
}
return true;
}
});