-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
232 lines (216 loc) · 6.65 KB
/
index.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
let buffer=[]
let tempNum = 0;
let newInput = false;
let isEq = false;
let eqBuffer = [];
let calcOrder = [];
function runCalculator() {
document.querySelector('.buttons')
.addEventListener("click", function(event) {
let inputVal = event.target.innerText;
console.log(inputVal);
action(inputVal);
displayNum();
})
document.querySelector('.buttons')
.addEventListener("mousedown", (event)=>{
actionStyleDown(event.target);
})
document.querySelector('.buttons')
.addEventListener("mouseup", (event)=>{
actionStyleUp(event.target);
})
opButtonStyle();
acButtonStyle();
}
function opButtonStyle() {
var opButtons = document.querySelectorAll('.primary-op');
opButtons.forEach(function(button) {
button.addEventListener("click", function() {
opButtons.forEach(function(btn) {
btn.classList.remove("primary-op--selected");
});
if (this.innerText != "=") {
this.classList.add("primary-op--selected");
}
});
});
}
function acButtonStyle() {
document.querySelector('.ac').addEventListener("click", function(event) {
event.target.innerText = "AC";
var opButtons = document.querySelectorAll('.primary-op');
opButtons.forEach(function(button) {
button.classList.remove("primary-op--selected");
});
})
document.querySelector('.buttons')
.addEventListener("click", function(event) {
var acButton = document.querySelector('.ac');
let inputVal = event.target.innerText;
if (inputVal != "AC" && isFinite(inputVal)) {
acButton.innerText = "C"
}
if (inputVal == "C") {
acButton.innerText = "AC"
}
})
}
function actionStyleDown(input) {
let inputVal = input.innerText;
if (inputVal == "+/-" || inputVal == "%" || inputVal == "AC" || inputVal == "C") {
input.classList.add("secondary-op--clicked");
}
if (inputVal == "÷" || inputVal == "+" || inputVal == "×" || inputVal == "–" || inputVal == "=") {
input.classList.add("primary-op--clicked");
} else {
input.classList.add("numeric--clicked")
}
}
function actionStyleUp(input) {
let inputVal = input.innerText;
if (inputVal == "+/-" || inputVal == "%" || inputVal == "AC" || inputVal == "C") {
input.classList.remove("secondary-op--clicked");
}
if (inputVal == "÷" || inputVal == "+" || inputVal == "×" || inputVal == "–" || inputVal == "=") {
input.classList.remove("primary-op--clicked");
} else {
input.classList.remove("numeric--clicked");
}
}
function action(inputVal) {
if (inputVal == "AC") {
buffer=[]
tempNum = 0;
newInput = false;
isEq = false;
eqBuffer = [];
calcOrder = [];
} else if (inputVal == "+/-") {
tempNum = -tempNum;
} else if (inputVal == "%") {
tempNum = tempNum / 100;
} else if (inputVal == ".") {
if (!newInput & !tempNum.toString().includes(".")) {
tempNum += inputVal;
} else {
tempNum = 0
tempNum += inputVal;
newInput = false;
}
} else if (isFinite(inputVal)) {
console.log("line_109", buffer);
console.log("line_110", isEq);
console.log("line_111", newInput)
if (!newInput & (tempNum != 0 || tempNum.toString().includes("."))) {
tempNum += inputVal;
} else {
tempNum = inputVal;
newInput = false;
}
} else if (inputVal == "=") {
if (!isEq) {
buffer.push(Number(tempNum));
} else {
buffer = buffer.concat(eqBuffer);
}
if (buffer.length > 2) {
tempNum = calcBuffer(buffer);
eqBuffer = buffer.slice(-2);
}
buffer = [Number(tempNum)];
isEq = true;
newInput = true;
console.log("line 131", buffer);
} else {
curr_op = opMapping(inputVal);
console.log("line 134", newInput);
console.log("line 135", buffer);
console.log("line 136", isEq);
if (newInput) {
// If isEq is true, we will only have one element in buffer:
if (!isEq) {
buffer.pop();
calcOrder.pop();
} else {
buffer = [];
buffer.push(Number(tempNum));
console.log("line 140", buffer);
}
} else {
// Push zero into buffer, if the calculator is just initiated.
if (isEq) {
buffer.pop();
}
buffer.push(Number(tempNum));
}
// Push the operation symbol into buffer
// console.log(calcOrder);
if (buffer.length >= 3) {
if (calcOrder.length == 2) {
if (calcOrder.toString() !== '-1,1') {
tempNum = calcBuffer(buffer);
buffer = [Number(tempNum)];
calcOrder = calcOrder.slice(1);
}
}
if (calcOrder.length == 3) {
if (calcOrder.toString() === '-1,1,-1') {
tempNum = calcBuffer(buffer);
buffer = [Number(tempNum)];
calcOrder = calcOrder.slice(2);
} else if (calcOrder.toString() === '-1,1,1') {
tempBuffer = buffer.slice(-3);
// console.log(tempBuffer);
tempNum = calcBuffer(tempBuffer);
buffer = buffer.slice(0, -3).concat([Number(tempNum)]);
calcOrder.splice(2, 1);
}
}
}
buffer.push(curr_op);
newInput = true;
isEq = false;
}
}
function calcBuffer(bufferArr) {
if (bufferArr.length == 1) {
return bufferArr[0];
}
curr_num = bufferArr[0];
curr_ops = bufferArr[1];
return curr_ops(curr_num, calcBuffer(bufferArr.slice(2)));
}
function opMapping(input) {
let op;
if (input == "÷") {
op = devide;
calcOrder.push(1);
} else if (input == "+") {
op = add;
calcOrder.push(-1);
} else if (input == "×") {
op = multiply;
calcOrder.push(1);
} else if (input == "–") {
op = minus;
calcOrder.push(-1);
}
return op;
}
function displayNum() {
document.querySelector('.results').innerText = tempNum;
}
function multiply(a, b) {
return a * b;
}
function add(a, b) {
return a + b;
}
function minus(a, b) {
return a - b;
}
function devide(a, b) {
return a / b;
}
runCalculator();