-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
368 lines (318 loc) · 10.7 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
function add(a, b) {
return a + b;
}
function subtract(a, b) {
// fix recurring decimals for subtracting float values
let dpA = 0;
let dpB = 0;
if (Math.floor(a) !== a) {
dpA = a.toString().split('.')[1].length;
}
if (Math.floor(b) !== b) {
dpB = b.toString().split('.')[1].length;
}
let dp = 0;
if (dpA >= dpB) {
dp = dpA;
} else {
dp = dpB;
}
return +(a - b).toFixed(dp);
}
function multiply(a, b) {
return +(a * b).toFixed(11);
}
function divide(a, b) {
if (b == 0 || b == -0) {
if (a == 0 || a == -0) {
return "Error";
}
return "Infinity";
} else {
let answer = a / b;
// count number of digits to check whether decimal place needs to be rounded
if (Math.floor(answer) !== answer) {
let digitCount = answer.toString().split('.').join('').length;
if (digitCount > 12) {
let dp = 12 - answer.toString().split('.')[0].length;
answer = +answer.toFixed(dp);
}
}
return answer;
}
}
function operate(num1, operator, num2) {
// check for calculations involving infinity
num2 = Number(num2);
if (num1 == "Infinity") {
if ((num2 == '0' || num2 == "-0") && operator == '*') {
return "Error";
}
return "Infinity";
}
num1 = Number(num1);
if (operator == '+') {
return add(num1, num2);
} else if (operator == '-') {
return subtract(num1, num2);
} else if (operator == '*') {
return multiply(num1, num2);
} else if (operator == '÷' || operator == '/') {
return divide(num1, num2);
}
}
function update_display_num(num_input) {
let current_num = document.querySelector('.calculator-display-inner').innerText;
if (current_num != "Error") {
// if last key pressed was operator, refresh display
if ((last_key != null) && (last_key.match(/[+\-*÷/=]/))) {
current_num = num_input;
}
// numbers with more than one digit or non decimals cannot start with 0
else if (current_num == '0') {
if (num_input != '0') {
current_num = num_input;
}
}
// negative zero
else if (current_num == "-0") {
if (num_input != '0') {
current_num = '-' + num_input;
}
}
else {
current_num += num_input;
}
document.querySelector('.calculator-display-inner').innerText = current_num;
last_key = num_input;
}
}
function update_display_op(op_input) {
let current_num = document.querySelector('.calculator-display-inner').innerText;
if (current_num != "Error") {
if (last_key == null) {
// if first button pressed is operator, assume first number is 0
saved_num = current_num.toString();
} else {
// store previous results to saved_num and show answer if operate can be called
if (!last_key.match(/[+\-*÷/]/)) {
if (saved_num == null) {
saved_num = current_num.toString(); // saved_num is stored in string format to account for Infinity and Error
} else {
saved_num = operate(saved_num, saved_op, current_num);
document.querySelector('.calculator-display-inner').innerText = saved_num;
}
}
}
// if result is Error, reset saved_num and saved_op
if (saved_num == "Error") {
saved_num = null;
saved_op = null;
} else {
saved_op = op_input;
}
last_key = op_input;
}
}
function update_display_decimal() {
let current_num = document.querySelector('.calculator-display-inner').innerText;
if (current_num != "Error") {
// if last key pressed was operator, refresh display
if ((last_key != null) && (last_key.match(/[+\-*÷/=]/))) {
current_num = "0.";
} else {
// check if there's already one decimal place in number
let length = current_num.length;
let i = 0;
while ((i < length) && (current_num[i] != '.')) {
i++;
}
if (i == length) {
current_num += '.';
}
}
document.querySelector('.calculator-display-inner').innerText = current_num;
last_key = '.';
}
}
function update_display_plusminus() {
let current_num = document.querySelector('.calculator-display-inner').innerText;
if (current_num != "Error") {
// if last key pressed was operator, refresh display
if ((last_key != null) && (last_key.match(/[+\-*÷/=]/))) {
current_num = "-0";
} else {
if (current_num[0] == '-') {
current_num = current_num.substring(1);
} else {
current_num = '-' + current_num;
}
}
document.querySelector('.calculator-display-inner').innerText = current_num;
last_key = '_';
}
}
function update_display_equal() {
let current_num = document.querySelector('.calculator-display-inner').innerText;
if (current_num != "Error") {
// check if operate can be called
if ((last_key.match(/[0-9._]/)) && (saved_num != null)) {
document.querySelector('.calculator-display-inner').innerText = operate(saved_num, saved_op, current_num);
// after last operation, refresh saved variables
saved_num = null;
saved_op = null;
}
last_key = '=';
}
}
function update_display_clear() {
document.querySelector('.calculator-display-inner').innerText = '0';
saved_num = null;
saved_op = null;
last_key = null;
}
function update_display_back() {
let current_num = document.querySelector('.calculator-display-inner').innerText;
if ((last_key != null) && (last_key.match(/[0-9._]/))) {
if (current_num.length <= 1 || current_num == "-0") {
current_num = '0';
}
else if (current_num.length == 2 && current_num[0] == '-') {
current_num = "-0";
}
else {
current_num = current_num.slice(0, -1);
}
document.querySelector('.calculator-display-inner').innerText = current_num;
}
}
let numbers = document.querySelectorAll('.number');
numbers.forEach(number => number.addEventListener('click', function() {
update_display_num(number.innerText);
}));
let operators = document.querySelectorAll('.operator');
operators.forEach(operator => operator.addEventListener('click', function() {
update_display_op(operator.innerText);
}));
let decimal = document.querySelector('.decimal');
decimal.addEventListener('click', function() {
update_display_decimal();
});
let plusminus = document.querySelector('.plusminus');
plusminus.addEventListener('click', function() {
update_display_plusminus();
});
let equal = document.querySelector('.equal');
equal.addEventListener('click', function() {
update_display_equal();
});
let clear = document.querySelector('.clear');
clear.addEventListener('click', function() {
update_display_clear();
});
let back = document.querySelector('.back');
back.addEventListener('click', function() {
update_display_back();
});
let saved_num = null;
let saved_op = null;
let last_key = null;
// keyboard support
document.addEventListener('keydown', function(event) {
// prevent repeat
if (!event.repeat) {
// numbers
if (!isNaN(event.key)) {
update_display_num(event.key);
numbers[`${event.key}`].classList.add("active");
}
// operators
else if (event.key.match(/[\+\-\*\/]/)) {
update_display_op(event.key);
// loop to find corresponding button
if (event.key === '/') {
document.getElementById("obelus").classList.add("active");
} else {
let k = event.key;
let i = 0;
let found = false;
while (i < 3 && !found) {
if (k == operators[i].innerHTML) {
operators[i].classList.add("active");
found = true;
}
i++;
}
}
}
// decimal
else if (event.key === '.') {
update_display_decimal();
document.querySelector('.decimal').classList.add("active");
}
// plusminus
else if (event.key === '_') {
update_display_plusminus();
document.querySelector('.plusminus').classList.add("active");
}
// equal
else if (event.key === '=' || event.key === "Enter") {
update_display_equal();
document.querySelector('.equal').classList.add("active");
}
// clear
else if (event.key === 'c' || event.key === 'C') {
update_display_clear();
document.querySelector('.clear').classList.add("active");
}
// back
else if (event.key === "Backspace") {
update_display_back();
document.querySelector('.back').classList.add("active");
}
}
});
// CSS purposes: remove darker button border when keyup
document.addEventListener('keyup', function(event) {
// numbers
if (!isNaN(event.key)) {
numbers[`${event.key}`].classList.remove("active");
}
// operators
else if (event.key.match(/[\+\-\*\/]/)) {
if (event.key === '/') {
document.getElementById("obelus").classList.remove("active");
} else {
let k = event.key;
let i = 0;
let found = false;
while (i < 3 && !found) {
if (k == operators[i].innerHTML) {
operators[i].classList.remove("active");
found = true;
}
i++;
}
}
}
// decimal
else if (event.key === '.') {
document.querySelector('.decimal').classList.remove("active");
}
// plusminus
else if (event.key === '_') {
document.querySelector('.plusminus').classList.remove("active");
}
// equal
else if (event.key === '=' || event.key === "Enter") {
document.querySelector('.equal').classList.remove("active");
}
// clear
else if (event.key === 'c' || event.key === 'C') {
document.querySelector('.clear').classList.remove("active");
}
// back
else if (event.key === "Backspace") {
document.querySelector('.back').classList.remove("active");
}
});