-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
344 lines (298 loc) · 11.5 KB
/
app.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
function handleAddition(x, y) {
return x + y;
}
function handleSubtraction(x, y) {
return x - y;
}
function handleMultiplication(x, y) {
return x * y;
}
function handleDivision(x, y) {
return x / y;
}
function handlePercentage() {
let currentDisplay = document.getElementById("current");
let currentNumber = Number(currentDisplay.textContent);
let newNumber = currentNumber/100;
currentDisplay.textContent = newNumber;
writeOperatorToGlobal("percentage", newNumber);
}
function handleRoot() {
let currentDisplay = document.getElementById("current");
let currentNumber = Number(currentDisplay.textContent);
let newNumber = Math.pow(currentNumber, 1/2);
currentDisplay.textContent = newNumber;
writeOperatorToGlobal("root", currentNumber);
}
function handleSquare() {
let currentDisplay = document.getElementById("current");
let currentNumber = Number(currentDisplay.textContent);
let newNumber = Math.pow(currentNumber, 2);
currentDisplay.textContent = newNumber;
writeOperatorToGlobal("square", currentNumber);
}
function handleFraction() {
let currentDisplay = document.getElementById("current");
let currentNumber = Number(currentDisplay.textContent);
let newNumber = 1/currentNumber;
currentDisplay.textContent = newNumber;
writeOperatorToGlobal("fraction", currentNumber);
}
function handleFactorial() {
let currentDisplay = document.getElementById("current");
let currentNumber = Number(currentDisplay.textContent);
let newNumber = 1;
for (let i = 2; i <= currentNumber; i++) {
newNumber = newNumber * i;
}
currentDisplay.textContent = newNumber;
writeOperatorToGlobal("factorial", currentNumber);
}
function writeOperatorToGlobal(operator, number, isOperatorOn=globalStates.advancedOperatorState) {
let previousDisplay = document.getElementById("previous");
if (!isOperatorOn) {
globalStates.advancedOperatorState = true;
switch(operator) {
case "percentage":
previousDisplay.textContent += `${number} `;
break;
case "root":
previousDisplay.textContent += `√(${number}) `;
break;
case "square":
previousDisplay.textContent += `sqr(${number}) `;
break;
case "fraction":
previousDisplay.textContent += `1/(${number}) `;
break;
case "factorial":
previousDisplay.textContent += `fact(${number}) `;
break;
}
} else {
let lastAdvancedOperator = findLastAdvancedOperator();
let globalText = previousDisplay.textContent
previousDisplay.textContent = globalText.slice(0, globalText.length - lastAdvancedOperator.length - 1);
writeOperatorToGlobal(operator, lastAdvancedOperator, false);
}
globalStates.operatorState = false;
}
function findLastAdvancedOperator() {
let previousDisplay = document.getElementById("previous").textContent;
previousDisplay = previousDisplay.split(" ");
return previousDisplay[previousDisplay.length-2];
}
function handleDigit(digit) {
let display = document.getElementById("current");
if ((display.textContent == 0 && !display.textContent.includes(".")) || globalStates.operatorState || globalStates.advancedOperatorState) {
display.textContent = "";
globalStates.operatorState = false;
if (globalStates.advancedOperatorState) {
globalStates.advancedOperatorState = false;
document.getElementById("previous").textContent = "";
}
}
display.textContent += digit;
}
function handleOperator(operatorType) {
switch (operatorType) {
case "add":
handleOperation("+");
break;
case "subtract":
handleOperation("-");
break;
case "multiply":
handleOperation("×");
break;
case "divide":
handleOperation("÷");
break;
case "equal":
handleEqual();
break;
case "clear":
handleClearDigit();
break;
case "clearAll":
handleClearAll();
break;
case "sign":
handleSignChange();
break;
case "percentage":
handlePercentage();
break;
case "root":
handleRoot();
break;
case "square":
handleSquare();
break;
case "fraction":
handleFraction();
break;
case "dot":
handleDecimalPoint();
break;
case "factorial":
handleFactorial();
break;
}
}
const globalStates = {
calculatorState: false,
operatorState: false, //Represents if the operator has been clicked
advancedOperatorState: false,
operator: "+",
number: 0,
accumulatorNumber: 0, //The number you accumulate to the global number while pressin "="
accumulatorState: false, //Represents if the accumulator is ON (if "=" is being used)
displayFontSize: getComputedStyle(document.getElementById("current")).fontSize //This is used when you use "clearAll" to reset it to default font size if it's changed
}
function handleEqual() {
if (globalStates.calculatorState) {
let currentDisplay = document.getElementById("current");
let currentNumber = Number(currentDisplay.textContent);
let globalNumber = globalStates.number;
let operationResult;
if (!globalStates.accumulatorState) {
document.getElementById("previous").textContent = "";
globalStates.accumulatorNumber = currentNumber;
globalStates.accumulatorState = true;
operationResult = handleMaths(globalStates.operator, globalNumber, globalStates.accumulatorNumber);
} else {
if (!globalStates.advancedOperatorState) { //If advanced operators are being clicked than the "equal" shouldn't keep activating them (they can only be used once)
operationResult = handleMaths(globalStates.operator, currentNumber, globalStates.accumulatorNumber);
} else {
operationResult = currentNumber;
}
}
globalStates.operatorState = true;
globalStates.advancedOperatorState = false;
currentDisplay.textContent = operationResult;
globalStates.number = operationResult;
}
}
function handleOperation(operator) {
writeToGlobal(operator);
if (!globalStates.operatorState) {
let globalNumber;
let currentDisplay = document.getElementById("current");
let currentNumber = Number(currentDisplay.textContent);
if (globalStates.accumulatorState) { // If accumulator ON reset the calculator so that you apply operator to the number in "current" instead of the number in accumulator
globalNumber = 0;
globalStates.accumulatorState = false;
globalStates.operatorState = true;
globalStates.operator = "+";
} else {
globalNumber = globalStates.number;
globalStates.operatorState = true;
}
let operationResult = handleMaths(globalStates.operator, globalNumber, currentNumber);
currentDisplay.textContent = operationResult;
globalStates.number = operationResult;
}
globalStates.operator = operator;
globalStates.advancedOperatorState = false;
globalStates.accumulatorState = false;
globalStates.calculatorState = true;
}
function writeToGlobal(operator) {
let previousDisplay = document.getElementById("previous");
let currentNumber = Number(document.getElementById("current").textContent);
if (!globalStates.advancedOperatorState) {
if (!globalStates.operatorState || globalStates.accumulatorState) {
if (currentNumber.toString()[0] === "-") {
previousDisplay.textContent += `(${currentNumber}) ${operator} `;
} else {
previousDisplay.textContent += `${currentNumber} ${operator} `;
}
} else {
previousDisplay.textContent = previousDisplay.textContent.slice(0, -2);
previousDisplay.textContent += `${operator} `;
}
} else {
previousDisplay.textContent += `${operator} `;
}
}
function handleMaths(operator, globalNumber, currentNumber) {
switch (operator) {
case "+":
return handleAddition(globalNumber, currentNumber);
case "-":
return handleSubtraction(globalNumber, currentNumber);
case "×":
return handleMultiplication(globalNumber, currentNumber);
case "÷":
return handleDivision(globalNumber, currentNumber);
}
}
function handleClearDigit() {
let display = document.getElementById("current");
(display.textContent.length === 1) ?
display.textContent = "0" :
display.textContent = display.textContent.slice(0, -1);
globalStates.operatorState = false;
globalStates.advancedOperatorState = false;
}
function handleClearAll() {
let currentDisplay = document.getElementById("current");
let previousDisplay = document.getElementById("previous");
currentDisplay.textContent = "0";
previousDisplay.textContent = "";
globalStates.operatorState = false;
globalStates.advancedOperatorState = false;
globalStates.operator = "+";
globalStates.number = 0;
globalStates.accumulatorNumber = 0;
globalStates.accumulatorState = false;
globalStates.calculatorState = false;
currentDisplay.style.fontSize = globalStates.displayFontSize;
}
function handleSignChange() {
let currentDisplay = document.getElementById("current");
let currentNumber = Number(currentDisplay.textContent);
currentNumber *= (-1);
currentDisplay.textContent = currentNumber;
globalStates.operatorState = false;
globalStates.advancedOperatorState = false;
}
function handleDecimalPoint() {
let currentDisplay = document.getElementById("current");
let currentDisplayContent = currentDisplay.textContent;
let currentNumber = Number(currentDisplay.textContent);
if (!currentDisplayContent.includes(".")) {
currentDisplay.textContent += "."
}
}
function handleDisplayOverflow() {
let currentDisplay = document.getElementById("current");
if (isDisplayOverflow(currentDisplay)) {
let displayFont = getComputedStyle(currentDisplay).fontSize;
displayFont = Number(parseFloat(displayFont).toFixed(2));
displayFont -= 5;
currentDisplay.style.fontSize = `${displayFont}px`;
}
}
function isDisplayOverflow(element) {
return element.scrollHeight > element.clientHeight || element.scrollWidth > element.clientWidth;
}
function convertPixelToEm(pixelVal) {
let bodyPixelSize = getComputedStyle(document.body).fontSize;
bodyPixelSize = Number(parseFloat(bodyPixelSize).toFixed(2));
let pixeValInNums = Number(parseFloat(pixelVal).toFixed(2))
let emValue = pixeValInNums / bodyPixelSize;
return Number(emValue.toFixed(2));
}
function handleButtonClick(e) {
if (e.target !== e.currentTarget) {
/digit/.test(e.target.className) ? //Checks if the clicked button is a digit or an operator
handleDigit(Number(e.target.id)) :
handleOperator(e.target.id);
handleDisplayOverflow()
}
e.stopPropagation();
}
let buttons = document.getElementById("buttons");
buttons.addEventListener("click", handleButtonClick);