-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
270 lines (221 loc) · 8.22 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
class Calculator {
//instantiate with output
constructor(previousOutputText, currentOutputText){
this.previousOutputText = previousOutputText;
this.currentOutputText = currentOutputText;
this.clear(); //sets everything to default values when new calculator is created
}
// clear all output, and reset operation!
clear() {
this.currentOperand = '';
this.previousOperand = '';
this.operation = undefined;
this.end = false;
this.updateDisplay();
}
//remove single number
delete() {
this.currentOperand = this.currentOperand.toString().slice(0,-1);
}
//add number selection to output
appendNumber(number){
if (number === '.' && this.currentOperand.toString().includes('.')) return; //prevent multiple periods
//if equalButton was used, then reset Calculator
if(this.end === true){
this.clear();
}
this.currentOperand = this.currentOperand.toString() + number.toString(); //deal with JS trying to sum numbers, instead of concat
}
// when equalButton is pressed, new numbers are NOT appended
// but operations can continue
logicalEnd() {
// deal with empty state scenario
// deal with interim state scenario (e.g. computing halfway, but press 'Equal')
if(this.operation === undefined || !this.currentOperand.length) return;
this.end = true;
}
//applies to /*-+ buttons
chooseOperation(operation){
//prevent situation where empty state shows operator in calculator
if(this.previousOperand === '' && this.currentOperand === '' || this.previousOperand === '' && this.currentOperand === '.') return;
//no reset, as user wishes to continue computations
if(this.end === true){
this.end = false;
}
//update previousOperand if differing operation is selected
if(this.currentOperand === '') {
this.operation = operation;
this.updateDisplay();
}
//otherwise, perform calculation
else
{
if(this.previousOperand !== '') {
// since previous isn't blank, perform computation
// for the purpose of updating 'previousOperand'
this.chooseOperationCompute();
}
//reference for further calculation, although it's reset in compute()
//facilitates equalButton
this.operation = operation;
this.previousOperand = this.currentOperand; //shift the output, since computed
this.currentOperand = '';
}
}
//differentiate computes between chooseOperation() and equalButton
chooseOperationCompute() {
this.compute();
}
equalButtonCompute(){
this.logicalEnd(); //run before compute() because of logic checks within logicalEnd()
this.compute();
this.updateDisplay();
}
// called by chooseOperation() and equalButton onclick
compute(){
let computation
const previous = parseFloat(this.previousOperand);
const current = parseFloat(this.currentOperand);
//compute if valid numbers
//addresses situation where Equals is clicked, but currentOperand is empty
if(isNaN(previous) || isNaN(current)) return;
switch(this.operation) {
case '+':
computation = previous + current;
break
case '-':
computation = previous - current;
break
case '*':
computation = previous * current;
break
case '/':
computation = previous / current;
break
default:
return
}
// console.log("computation: " + computation%1); //use mod 1 to determine float!
this.currentOperand = computation;
this.operation = undefined; //reset the operation /*-+ once done computing
this.previousOperand = '';
}
//beautify the number
getDisplayNumber(number) {
const stringNumber = number.toString(); //use string to perform split
const integerDigits = parseFloat(stringNumber.split('.')[0]) //take the first item
const decimalDigits = stringNumber.split('.')[1]
let integerDisplay;
if (isNaN(integerDigits)) {
integerDisplay = '';
}else{
integerDisplay = integerDigits.toLocaleString('en', {maximumFractionDigits: 0})
}
if (decimalDigits != null) {
return `${integerDisplay}.${decimalDigits}`;
}else{
return integerDisplay;
}
}
joke(){
this.previousOutputText.innerText = 'I\'d tell a joke,';
this.currentOutputText.innerText = 'but it never ends.';
}
//called by every button click!
updateDisplay(){
// function utilises currentOperand and previousOperand, which are working values within Calculator class
this.currentOutputText.innerText =
this.getDisplayNumber(this.currentOperand);
// rmb tt this.operation holds onto /*-+ signs
if(this.operation != null){
this.previousOutputText.innerText =
`${this.getDisplayNumber(this.previousOperand)} ${this.operation}`
}else{
this.previousOutputText.innerText = ''
}
// deal with infinity
if(this.currentOperand === Infinity || this.previousOperand === Infinity) {
this.joke();
}
}
numberButton(text){
this.appendNumber(text);
this.updateDisplay();
}
deleteButton() {
calculator.delete();
calculator.updateDisplay();
}
operationButton(text){
calculator.chooseOperation(text);
calculator.updateDisplay();
}
operate(e){
// console.log(e);
switch(e.key) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case '.':
calculator.numberButton(e.key)
break
case 'Delete':
calculator.deleteButton();
break
case '*':
case '/':
case '+':
case '-':
calculator.operationButton(e.key);
break
case 'Escape':
calculator.clear();
break
case 'Enter':
calculator.equalButtonCompute();
break
default:
return
}
}
}
// select all the relevant buttons and output
// using DATA attributes, which helps differentiate from CSS classes
const numberButtons = document.querySelectorAll('[data-number]');
const operationButtons = document.querySelectorAll('[data-operation]');
const equalButton = document.querySelector('[data-equals]');
const deleteButton = document.querySelector('[data-delete]');
const allClearButton = document.querySelector('[data-all-clear]');
const previousOutputText = document.querySelector('[data-previous-output]');
const currentOutputText = document.querySelector('[data-current-output]');
// make a new calculator
// with blank text as website loads
const calculator = new Calculator(previousOutputText, currentOutputText)
// take number in button and append it
numberButtons.forEach(button => {
button.addEventListener('click', () => {
calculator.numberButton(button.innerText);
})
})
operationButtons.forEach(button => {
button.addEventListener('click', () => {
calculator.operationButton(button.innerText);
})
})
equalButton.addEventListener('click', button => {
calculator.equalButtonCompute();
})
allClearButton.addEventListener('click', button => {
calculator.clear();
})
deleteButton.addEventListener('click', button => {
calculator.deleteButton();
})
window.addEventListener('keydown', calculator.operate);