-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
100 lines (88 loc) · 2.42 KB
/
script.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
function updateHistory(){
document.getElementById("history-value").innerText=calHistory;
}
function updateOutput(){
document.getElementById("output-value").innerText=calOutput;
}
function clearHistory(){
calHistory="";
updateHistory();
}
function clearOutput(){
calOutput="";
updateOutput();
}
var isFinished = false;
var calOutput ='';
var calHistory='';
updateOutput();
updateHistory();
var numKeys = document.getElementsByClassName("number");
for(var i = 0;i<numKeys.length;i++){
numKeys[i].addEventListener('click',function(){
if(isFinished){
clearHistory();
clearOutput();
}
else if(calOutput.length<18){
calOutput = calOutput + this.id;
}
updateOutput();
isFinished=false;
});
}
var opeKeys = document.getElementsByClassName("operator");
for(var i = 0;i<opeKeys.length;i++){
opeKeys[i].addEventListener('click',function(){
if(calOutput!=""){
if(isFinished){
clearHistory();
}
calHistory = calHistory + calOutput + this.value;
updateHistory();
clearOutput();
isFinished=false;
}
});
}
var calcKey = document.getElementById("equal");
calcKey.addEventListener('click',function(){
if(calOutput!=""){
if(calHistory!=""){
calHistory = calHistory + calOutput;
calOutput=eval(calHistory);
updateOutput();
calHistory = calHistory + "=";
updateHistory();
isFinished=true;
}
}
});
var backKey = document.getElementById("backspace");
backKey.addEventListener('click',function(){
if(calOutput!=""){
if(!isFinished){
calOutput=calOutput.substr(0,calOutput.length-1);
updateOutput();
}
}
});
var clearKey= document.getElementById("clear");
clearKey.addEventListener('click',function(){
clearHistory();
clearOutput();
});
var negKey= document.getElementById("negToggle");
negKey.addEventListener('click',function(){
if(calOutput!=""){
if(!isFinished){
if(calOutput[0]=="-"){
calOutput=calOutput.substr(1,calOutput.length-1);
}
else{
calOutput="-"+calOutput;
}
updateOutput();
}
}
});