-
Notifications
You must be signed in to change notification settings - Fork 101
/
calc.js
80 lines (73 loc) · 2.1 KB
/
calc.js
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
var numKeys = [];
var opns = [];
var res = document.querySelector("#resultArea");
var clrBtn = document.querySelector("#clrTxt");
var delBtn = document.querySelector("#del");
var eqBtn = document.querySelector("#eq");
var decPoint = document.querySelector("#decp");
var opac=document.querySelector("#opac");
var opnSyms = ["+", "-", "*", "/"];
var symList;
var tmpCal;
var j;
var duplicate;
for(var i=0;i<=9;i++){
(function(i){
qs = "#num" + i;
numKeys.push(document.querySelector(qs));
numKeys[i].addEventListener("click", function(){
res.textContent += i;
if(opnSyms.length>=1){
opac.innerHTML=eval(res.textContent);
}
});
}(i));
}
for(var i=0;i<=3;i++){
(function(i){
qs = "#op" + i;
opns.push(document.querySelector(qs));
opns[i].addEventListener("click", function(){
res.textContent += opnSyms[i];
});
}(i));
}
clrBtn.addEventListener("click", function(){
res.textContent = "";
opac.innerHTML="";
});
delBtn.addEventListener("click", function(){
temp=res.textContent[res.textContent.length-1];
res.textContent = res.textContent.substring(0, res.textContent.length - 1);
if((temp!="+") && temp!="-" && temp!="*" && temp!="/"){
duplicate=res.textContent.substring(0,res.textContent.length - 1);
if(res.textContent[res.textContent.length-1]!="+" && res.textContent[res.textContent.length-1]!="-" && res.textContent[res.textContent.length-1]!="*" && res.textContent[res.textContent.length-1]!="/"){
opac.innerHTML=eval(res.textContent);
}
else{
duplicate=res.textContent.substring(0,res.textContent.length - 1);
opac.innerHTML=eval(duplicate);
}
}
});
decPoint.addEventListener("click", function(){
res.textContent += ".";
});
eqBtn.addEventListener("click", function(){
(function(i, j){
symList = [];
for(var i=0; i<opnSyms.length; i++){
if(res.textContent.indexOf(opnSyms[i]) > -1){
symList.push(opnSyms[i]);
}
}
if(symList.length > 0){
try{
res.textContent = eval(res.textContent);
}
catch(e){
res.textContent = "Invalid Syntax";
}
}
}(i, j));
});