-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
367 lines (338 loc) · 14.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
'use strict';
(function applicationWrapper() {
let arg1 = '0';
let arg2;
let operator;
let result;
let repeatWithSameArgAndOperator;
let repeatValue;
let lastBtnPressWasNumberOrDecimalPoint = true;
initializeApplication();
function initializeApplication() {
let btns = Array.from(document.querySelectorAll('button'));
addClickEventListenersToButtons();
populateDisplay(arg1);
document.addEventListener('keydown', keyboardHandler);
document.addEventListener('keyup', keyboardHandler);
function addClickEventListenersToButtons() {
for (let btn of btns) {
const btnValue = btn.dataset.value;
const numbers = ['0','1', '2', '3', '4', '5', '6', '7', '8', '9'];
const operators = ['+', '-', '*', '/'];
if (numbers.includes(btnValue)) {
btn.addEventListener('click', function() {
handleNumber(btnValue);
});
}
else if (operators.includes(btnValue)) {
btn.addEventListener('click', function() {
handleOperator(btnValue);
})
}
else if (btnValue === '.') {
btn.addEventListener('click', handleDecimal);
}
else if (btnValue === '=') {
btn.addEventListener('click', handleEquals);
}
else if (btnValue === 'AC') {
btn.addEventListener('click', handleClear);
}
else if (btnValue === 'Backspace') {
btn.addEventListener('click', handleBackspace);
}
function handleNumber(x) {
lastBtnPressWasNumberOrDecimalPoint = true;
result = undefined;
if (typeof arg1 === 'undefined') {
arg1 = x;
populateDisplay(arg1);
}
else if (typeof operator === 'undefined') {
modifyArg(1, x);
populateDisplay(arg1);
}
else if (typeof arg2 === 'undefined') {
arg2 = x;
populateDisplay(arg2);
}
else {
modifyArg(2, x);
populateDisplay(arg2);
}
console.log({
arg1,
arg2,
operator,
result,
repeatWithSameArgAndOperator,
repeatValue
});
function modifyArg(operand, value) {
if (operand === 1) {
/* if arg1 is already 20 chars long, do nothing (to
avoid storing numbers that are too long) */
if(arg1.length === 20) return;
else if(arg1 === '0') arg1 = value;
else arg1 += value;
}
else if (operand === 2) {
/* if arg2 is already 20 chars long, do nothing (to
avoid storing numbers that are too long) */
if(arg2.length === 20) return;
else if(arg2 === '0') arg2 = value;
else arg2 += value;
}
}
}
function handleOperator(newOp) {
lastBtnPressWasNumberOrDecimalPoint = false;
if (typeof result !== 'undefined') {
arg1 = result;
result = undefined;
operator = newOp;
}
if (typeof arg1 === 'undefined') {
/* an operator can't do anything by itself, it needs an
operand.... so do nothing */
return;
}
else if (typeof arg2 === 'undefined') {
operator = newOp;
}
else {
// there is an arg2 already defined
result = calculate();
populateDisplay(result);
operator = newOp;
arg1 = result;
arg2 = undefined;
}
console.log({
arg1,
arg2,
operator,
result,
repeatWithSameArgAndOperator,
repeatValue
});
}
function handleDecimal() {
lastBtnPressWasNumberOrDecimalPoint = true;
if (typeof arg1 === 'undefined') {
arg1 = '.';
populateDisplay(arg1);
}
else if (typeof operator === 'undefined') {
if (arg1.includes('.') || (arg1.length >= 19)) {
/* if arg1 is already 19 chars long, there is no
room for a decimal point and another digit, so don't
do anything. Also, if arg1 already has a decimal
point, it can't accept another */
return;
}
arg1 += '.';
populateDisplay(arg1);
}
else if (typeof arg2 === 'undefined') {
arg2 = '.';
populateDisplay(arg2);
}
else if (arg2.includes('.') || (arg2.length >= 19)) {
return;
}
else {
arg2 += '.';
populateDisplay(arg2);
}
}
function handleEquals() {
lastBtnPressWasNumberOrDecimalPoint = false;
if (typeof arg1 === 'undefined') {
repeatWithSameArgAndOperator = false;
repeatValue = undefined;
return;
}
else if (typeof operator === 'undefined') {
/* continue to show the previously entered number on the
screen, but delete arg1 so that user can "start fresh"
by entering a new arg1 */
arg1 = undefined;
repeatWithSameArgAndOperator = false;
repeatValue = undefined;
}
else if (typeof arg2 === 'undefined') {
/* if there is one number in the equation and the user
has already entered an operator, but then hits the equals
button, perform the operation using the same number for
arg2 as for arg1. Ex: User hits "5", then "+", followed
by "=". The calculator will perform 5 + 5 and show the
result. If the user presses equals again, the calculator
will add 5 to the result of the previous equation, and it
will show 15. User presses equals again, will show 20.
Etc, etc. */
if (!repeatWithSameArgAndOperator) {
repeatValue = arg1;
result = calculate();
populateDisplay(result);
arg1 = result;
repeatWithSameArgAndOperator = true;
/* this flag will set the calculator to be in "repeat
mode", which performs the action described in the
above comment every time the equals button is
pressed, until the user breaks the pattern of
repeatedly pressings equals, at which point the flag
will be set to false, and the calculator will no
longer be in "repeat mode" */
}
else if (repeatWithSameArgAndOperator) {
result = calculate();
populateDisplay(result);
arg1 = result;
}
}
else {
result = calculate();
populateDisplay(result);
operator = undefined;
arg1 = undefined;
arg2 = undefined;
repeatValue = false;
repeatValue = undefined;
}
console.log({
arg1,
arg2,
operator,
result,
repeatWithSameArgAndOperator,
repeatValue
});
}
function handleClear() {
lastBtnPressWasNumberOrDecimalPoint = false;
arg2 = undefined;
operator = undefined;
result = undefined;
repeatWithSameArgAndOperator = undefined;
repeatValue = undefined;
arg1 = '0';
populateDisplay(arg1);
}
function handleBackspace(){
if (lastBtnPressWasNumberOrDecimalPoint) {
if (typeof arg2 !== 'undefined') {
if (arg2.length > 1) {
arg2 = arg2.slice(0, (arg2.length - 1));
populateDisplay(arg2);
}
else {
arg2 = undefined;
populateDisplay('');
}
}
else if (typeof arg1 !== "undefined") {
if (arg1.length > 1) {
arg1 = arg1.slice(0, (arg1.length - 1));
populateDisplay(arg1);
}
else {
arg1 = undefined;
populateDisplay('');
}
}
}
else return;
}
}
}
function keyboardHandler(e) {
const key = e.key;
if (key === 'Escape' || key === 'Delete') {
let clearBtn = btns.find(btn => btn.dataset.value === 'AC');
if (e.type === 'keydown') {
clearBtn.classList.add('active');
clearBtn.click();
}
else if (e.type === 'keyup') clearBtn.classList.remove('active');
}
else if (key === 'Enter') {
let equalsBtn = btns.find(btn => btn.dataset.value === '=');
if (e.type === 'keydown') {
equalsBtn.classList.add('active');
equalsBtn.click();
}
else if (e.type === 'keyup') equalsBtn.classList.remove('active');
}
else {
const correspondingBtn = btns.find(btn => btn.dataset.value === key);
if (correspondingBtn) {
if (e.type === 'keydown') {
correspondingBtn.classList.add('active');
correspondingBtn.click();
}
else if (e.type === 'keyup') {
correspondingBtn.classList.remove('active');
}
}
}
}
}
function calculate() {
let result;
if (operator === '/' && arg2 === '0') result = 'EXPLODES';
else if (repeatValue) result = operate(+arg1, +repeatValue, operator);
else result = operate(+arg1, +arg2, operator);
return result.toString();
function operate(x, y, operator) {
switch (operator) {
case '+':
return add(x, y);
break;
case '-':
return subtract(x, y);
break;
case '*':
return multiply(x, y);
break;
case '/':
return divide(x, y);
break;
}
function add(x, y) {
return x + y;
}
function subtract(x, y) {
return x-y;
}
function multiply(x, y) {
return x * y;
}
function divide(x, y) {
return x / y;
}
}
}
function populateDisplay(value) {
const displayDiv = document.querySelector('div.display');
displayDiv.textContent = formatToMax20Chars(value);
function formatToMax20Chars(str) {
if (str.length > 20 && ((Number(str) > 1e19))) {
// if the number is huge, display it in scientific notation
str = Number(str).toExponential(14);
/* the scientific notation will show only 14 digits after the
decimal point, so that at the most, the result (string) will be
a max of twenty characters long, such as 9.999999999989e+24 */
}
else if (str.length > 20) {
/* else, if the string is too long b/c the number has too many
decimal places, reduce the string to 20 chars max */
str = str.slice(0, 20);
if (str[19] === '.') str = str.slice(0,19);
/* if the last char would be a decimal point, cut off that
decimal point as well */
}
return str;
}
}
})();