-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
314 lines (270 loc) · 7.58 KB
/
main.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
let themeToggle = document.querySelector(".theme-toggle");
let currentTheme = localStorage.getItem("theme");
if (!currentTheme) {
// os default
var preference = window.matchMedia("(prefers-color-scheme: dark)");
if (preference.matches) {
currentTheme = "dark";
} else {
currentTheme = "light";
}
}
if (currentTheme === "dark") toggleTheme(false);
function toggleTheme(setLocalStorage = true) {
document.body.classList.toggle("dark-mode");
if (document.body.classList.contains("dark-mode")) {
themeToggle.textContent = "Light Mode";
if (setLocalStorage) localStorage.setItem("theme", "dark");
} else {
themeToggle.textContent = "Dark Mode";
if (setLocalStorage) localStorage.setItem("theme", "light");
}
}
//
function executeFunc(calcBodyFunction) {
if (calcBodyFunction.isFourFunc) {
if (currNum) {
prevNum = currBodyFunction.func();
currNum = null;
currBodyFunction = null;
}
if (currBodyFunction) {
op = currBodyFunction;
currBodyFunction = null;
op.func();
currBodyFunction = calcBodyFunction;
} else {
currBodyFunction = calcBodyFunction;
}
} else {
calcBodyFunction.func();
}
}
function add() {
return (Number(prevNum) + Number(currNum)).toString();
}
function subtract() {
return (Number(prevNum) - Number(currNum)).toString();
}
function multiply() {
return (Number(prevNum) * Number(currNum)).toString();
}
function divide() {
if (Number(currNum) === 0) {
handleError("Tried to divide by 0", true);
return "0";
} else {
return (Number(prevNum) / Number(currNum)).toString();
}
}
function addDecimal() {
if (currNum) {
if (!currNum.includes(".")) {
currNum += ".";
}
} else if (prevNum) {
if (!prevNum.includes(".")) {
prevNum += ".";
}
}
}
function equals() {
if (currBodyFunction && !currNum) {
handleError("Can't use = when there is no second number", false);
} else {
if (currBodyFunction) {
executeFunc(currBodyFunction);
currBodyFunction = null;
} else {
// do nothing
}
}
}
function clear() {
prevNum = "0";
currBodyFunction = null;
currNum = null;
}
function back() {
if (currNum && currNum !== "0") {
currNum = currNum.slice(0, currNum.length - 1);
if (currNum === "") {
currNum = "0";
}
} else if (prevNum !== "0" && !currBodyFunction) {
prevNum = prevNum.slice(0, prevNum.length - 1);
if (prevNum === "") {
prevNum = "0";
}
}
}
function negate() {
if (currNum && currNum !== "0") {
currNum = (-1 * Number(currNum)).toString();
} else if (prevNum !== "0" && !currBodyFunction) {
prevNum = (-1 * Number(prevNum)).toString();
}
}
//
let prevNum = "0";
let currNum = null;
let currBodyFunction = null;
function operate(classList) {
if (classList[1] === "body-function") {
calcBodyFunction = calcBodyFunctions.find(
(calcBody) => calcBody.name === classList[0]
);
executeFunc(calcBodyFunction);
} else if (classList[1] === "digit") {
const digit = classList[0].slice(-1);
if (currBodyFunction) {
if (currNum) {
if (currNum === "0") {
currNum = digit;
} else {
if ((roundDisplayNumber(prevNum, 6) + currNum).length < 24) {
currNum += digit;
} else {
handleError("Maximum length of digit reached", false);
}
}
} else {
currNum = digit;
}
} else {
if (prevNum === "0") {
prevNum = digit;
} else {
if (prevNum.length < 24) {
prevNum += digit;
} else {
handleError("Maximum length of digit reached", false);
}
}
}
}
}
//
function updateDisplay() {
const displayP = document.querySelector(".display-p");
if (currBodyFunction) {
displayP.textContent = roundDisplayNumber(prevNum, 6);
displayP.textContent += " " + currBodyFunction.value;
if (currNum) {
displayP.textContent += " " + currNum; // roundDisplayNumber(currNum, 6);
}
} else {
displayP.textContent = prevNum;
}
}
function roundDisplayNumber(num, decimalPlaces) {
let newNum = String(+parseFloat(num).toFixed(decimalPlaces));
if (newNum.includes("e")) {
let numBefore = newNum.split("e")[0];
let numAfter = "e" + newNum.split("e")[1];
numBefore = String(+parseFloat(numBefore).toFixed(decimalPlaces));
newNum = numBefore + numAfter;
} else {
if (num.includes(".") && !newNum.includes(".")) {
newNum += ".";
}
}
return newNum;
}
let timeoutID = null;
function handleError(eMsg, reset) {
const errorDialog = document.querySelector(".error-dialog");
errorDialog.textContent = eMsg;
errorDialog.classList.add("show");
if (timeoutID) {
clearTimeout(timeoutID);
}
timeoutID = setTimeout(() => errorDialog.classList.remove("show"), 3000);
if (reset) {
prevNum = "0";
currNum = null;
currBodyFunction = null;
}
}
//
function CalcFunc(name, value, keyMapping, func, isFourFunc) {
this.name = name; // used to link class to function
this.value = value; // used for display
this.keyMapping = keyMapping; // used to link keystroke to function
this.func = func; // the actual function
this.isFourFunc = isFourFunc; // is four func or not
}
const calcBodyFunctions = [
new CalcFunc("add", "+", ["+"], add, true),
new CalcFunc("subtract", "−", ["-"], subtract, true),
new CalcFunc("multiply", "×", ["*"], multiply, true),
new CalcFunc("decimal", ".", ["."], addDecimal, false),
new CalcFunc("equals", "=", ["=", "Enter"], equals, false),
new CalcFunc("divide", "÷", ["/"], divide, true),
new CalcFunc("clear", "", ["c"], clear, false),
new CalcFunc("back", "", ["Backspace"], back, false),
new CalcFunc("negate", "", ["n"], negate, false),
];
//
function createCalcBody() {
const calcDiv = document.querySelector(".calc");
let startDigit = 7;
let startFunction = 0;
for (let row = 0; row < 4; row++) {
if (row === 3) {
startDigit = 0;
}
for (let col = 0; col < 4; col++) {
const newButton = document.createElement("button");
if (col === 3 || (row === 3 && col > 0)) {
const func = calcBodyFunctions[startFunction];
newButton.textContent = func["value"];
newButton.classList.add(func["name"]);
newButton.classList.add("body-function");
newButton.classList.add("main-grid");
startFunction++;
} else {
newButton.textContent = startDigit;
newButton.classList.add(`digit-${startDigit}`);
newButton.classList.add("digit");
newButton.classList.add("main-grid");
startDigit++;
}
calcDiv.appendChild(newButton);
}
startDigit -= 6;
}
const allButtons = document.querySelectorAll("button");
for (const button of allButtons) {
button.onclick = function () {
operate(button.classList);
updateDisplay();
};
}
}
createCalcBody();
//
body = document.querySelector("body");
body.onkeydown = function (e) {
calcBodyFunction = calcBodyFunctions.find((calcBody) =>
calcBody.keyMapping.includes(e.key)
);
if (!calcBodyFunction && e.key.match(/^[0-9]+$/) == null) {
return;
}
let button;
if (calcBodyFunction) {
button = document.querySelector(`.${calcBodyFunction.name}`);
operate([calcBodyFunction.name, "body-function"]);
} else if (e.key.match(/^[0-9]+$/) != null) {
button = document.querySelector(`.digit-${e.key}`);
operate([e.key, "digit"]);
}
updateDisplay();
button.classList.add("active");
setTimeout(() => button.classList.remove("active"), 100);
};
//
themeToggle.onclick = function () {
toggleTheme();
};