-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
241 lines (186 loc) · 6.06 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
const mainScreen = document.querySelector(".main-screen");
const topScreen = document.querySelector(".top-screen");
const numButtons = document.querySelectorAll(".num");
const equalButton = document.querySelector(".equal");
const opButtons = document.querySelectorAll(".operator");
const clearButtons = document.querySelectorAll(".clear");
const plusMinus = document.querySelector(".plus-minus");
let inOps = false;
let isEval = false;
numButtons.forEach((button) => {
button.addEventListener("click", displayNum);
});
opButtons.forEach((button) => {
button.addEventListener("click", displayOperator);
});
clearButtons.forEach((button) => {
button.addEventListener("click", clear);
});
equalButton.addEventListener("click", displayEval);
plusMinus.addEventListener("click", changeSign);
window.addEventListener("keydown", handleKeys);
function displayEval() {
let calc = new Calculator;
let result = 0
if (topScreen.textContent === "0" || !inOps) {
result = calc.calculate(mainScreen.textContent);
} else {
topScreen.textContent += mainScreen.textContent;
result = calc.calculate(topScreen.textContent);
}
mainScreen.textContent = result;
inOps = false;
isEval = true;
}
function displayOperator() {
let result = 0
let operator = this.textContent;
if (operator == "-" && (isNaN(mainScreen.textContent) || mainScreen.textContent == "0")) {
changeSign();
return;
}
if (topScreen.textContent == "" || !inOps) {
topScreen.textContent = mainScreen.textContent + " " + this.textContent + " ";
} else {
let calc = new Calculator;
topScreen.textContent += mainScreen.textContent;
result = calc.calculate(topScreen.textContent);
topScreen.textContent = result + " " + this.textContent + " ";
}
mainScreen.textContent = 0;
inOps = true;
isEval = false;
}
function displayNum() {
if (mainScreen.textContent == "0" || isEval) {
mainScreen.textContent = this.textContent;
} else if (mainScreen.textContent.length < 8) {
mainScreen.textContent += this.textContent;
}
if (isEval) {
topScreen.textContent = "";
}
isEval = false;
}
function clear() {
if (this.classList.contains("all")) {
mainScreen.textContent = "0";
topScreen.textContent = "";
} else if (this.classList.contains("entry") && mainScreen.textContent !== "0") {
if (/^[0-9.]+$/.test(mainScreen.textContent)) {
mainScreen.textContent = mainScreen.textContent.slice(0, -1);
} else {
mainScreen.textContent = "";
}
if (mainScreen.textContent === "") {
mainScreen.textContent = "0"
}
}
}
function changeSign() {
let num = mainScreen.textContent;
if (num === "0") {
mainScreen.textContent = "-";
} else if (num === "-") {
mainScreen.textContent = "0";
} else if (num.startsWith("-")) {
mainScreen.textContent = num.substring(1);
} else {
mainScreen.textContent = `-${num}`;
}
}
function addDecimals(a, b) {
a = String(a);
b = String(b);
const adec = (a.split(".")[1] || "").length;
const bdec = (b.split(".")[1] || "").length;
const maxDec = Math.max(adec, bdec);
let divdec = "1";
divdec = divdec.padEnd(maxDec, "0");
return ((+a * divdec + +b * divdec) / divdec).toFixed(maxDec);
}
function multiplyDecimals(a, b) {
a = String(a);
b = String(b);
const adec = (a.split(".")[1] || "").length;
const bdec = (b.split(".")[1] || "").length;
const totalDec = adec + bdec;
return (a * b).toFixed(totalDec)
}
function divideDecimals(a, b) {
a = String(a);
b = String(b);
const adec = (a.split(".")[1] || "").length;
const bdec = (b.split(".")[1] || "").length;
const maxDec = Math.max(adec, bdec);
const divisor = Math.pow(10, maxDec);
return(a * divisor) / (b * divisor);
}
function Calculator(str) {
isEval = true;
this.methods = {
"-": (a, b) => addDecimals(a, -b),
"+": (a, b) => addDecimals(a, b),
"/": (a, b) => divideDecimals(a, b),
"*": (a, b) => multiplyDecimals(a, b)
};
this.calculate = function(str) {
let parts = str.split(" ");
a = +parts[0];
op = parts[1];
b = +parts[2];
if ((a || a == "0") && !op && !b) {
return formatResult(a);
}
if (isNaN(a) || isNaN(b)) {
return NaN;
}
if (!this.methods[op]) {
return "Operation not supported."
}
let result = this.methods[op](a, b);
return formatResult(result);
};
}
function formatResult(result) {
const resultStr = result.toString();
const maxLength = 9;
if (resultStr.length > maxLength) {
expoResult = parseFloat(result).toExponential(maxLength - 6);
return expoResult;
}
return result;
}
function handleKeys(e) {
const operators = {
"+": "plus",
"-": "minus",
"*": "multiply",
"/": "divide"
};
let button;
if (e.key >= 0 && e.key <= 9) {
button = document.querySelector(`#num${e.key}`);
} else if (e.key === ".") {
button = document.querySelector("#dec");
} else if (e.key === "Backspace" || e.key === "Delete") {
button = document.querySelector(".entry");
} else if (e.key === "c" || e.key === "C" || e.key === "Escape") {
button = document.querySelector(".all");
} else if (operators.hasOwnProperty(e.key)) {
e.preventDefault();
button = document.querySelector(`#${operators[e.key]}`);
} else if (e.key === "=" || e.key === "Enter") {
e.preventDefault();
button = document.querySelector(".equal");
} else if (e.key === "_") {
button = document.querySelector(".plus-minus");
}
if (button) {
button.classList.add("active");
window.addEventListener("keyup", () => {
button.classList.remove("active")
});
button.click();
}
}