-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
196 lines (185 loc) · 6.24 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
const MAXIMUM_SCREEN_LENGTH = 25;
let canOverwrite = false;
let ans = "0";
const buttons = document.querySelector(".buttons");
const currentScreen = document.querySelector(".current");
const resultScreen = document.querySelector(".result");
const equalBtn = document.querySelector("#equal");
const deleteBtn = document.querySelector("#delete");
const clearBtn = document.querySelector("#clear");
const signToggleBtn = document.querySelector("#plus-minus");
buttons.addEventListener("click", e => {
if (e.target.id === "equal" || e.target.id === "delete" ||
e.target.id === "clear" || e.target.id === "plus-minus" ||
e.target.className === "buttons") { // ignore special operations buttons
return;
}
appendInput(e.target.textContent);
});
equalBtn.addEventListener("click", e => {
// equal works in places where operators can work
// the addition operator (+) is chosen as a substitute for equal in checkExp()
let exp = `${currentScreen.textContent} + `;
exp = exp.replace(" ", " ");
if (checkExp(exp)) {
ans = resultScreen.textContent = calculate(currentScreen.textContent);
if (ans === "Silksong release date") {
ans = "0";
}
canOverwrite = true;
}
});
deleteBtn.addEventListener("click", e => {
canOverwrite = false;
const lastChar = currentScreen.textContent[currentScreen.textContent.length - 1];
if (lastChar === " " || lastChar === "s") { // operator or ans
currentScreen.textContent = currentScreen.textContent.slice(0, -3);
}
else {
currentScreen.textContent = currentScreen.textContent.slice(0, -1);
}
if (currentScreen.textContent.length === 0) {
currentScreen.textContent = "0";
}
});
clearBtn.addEventListener("click", e => {
currentScreen.textContent = "0";
resultScreen.textContent = "";
canOverwrite = false;
});
signToggleBtn.addEventListener("click", e => {
const lastCharIndex = currentScreen.textContent.length - 1;
if (currentScreen.textContent[lastCharIndex] === "s" ||
currentScreen.textContent[lastCharIndex] === " ") { // ans or operator
return;
}
canOverwrite = false;
let i = lastCharIndex;
while (i >= 0 && currentScreen.textContent[i] !== " ") {
i--;
}
i++;
const signToggledNumber = (+currentScreen.textContent.slice(i)) * -1;
currentScreen.textContent = currentScreen.textContent.slice(0, i) +
signToggledNumber;
});
function appendInput(input) {
if (currentScreen.textContent.length > MAXIMUM_SCREEN_LENGTH && !canOverwrite) {
return;
}
if (input === "×" || input === "÷" || input === "+" || input === "-") {
if (canOverwrite) {
currentScreen.textContent = `ans ${input} `;
canOverwrite = false;
return;
}
let exp = `${currentScreen.textContent} ${input} `;
// replace two spaces caused by two adjacent operators by one space
// to ensure that the split function works properly in checkExp()
exp = exp.replace(" ", " ");
if (checkExp(exp)) {
currentScreen.textContent = exp;
}
}
else if (input === ".") {
if (canOverwrite) {
return;
}
let exp = currentScreen.textContent + input;
if (checkExp(exp)) {
currentScreen.textContent = exp;
}
}
else { // number or ans
if (canOverwrite) {
currentScreen.textContent = input;
canOverwrite = false;
return;
}
if (currentScreen.textContent === "0") {
currentScreen.textContent = input;
return;
}
let exp = currentScreen.textContent + input;
if (checkExp(exp)) {
currentScreen.textContent = exp;
}
}
}
function calculate(exp) {
let tokenizedExp = exp.split(" ");
tokenizedExp = tokenizedExp.map(token => token === "ans" ? ans : token);
let result;
for (let i = 0; i < tokenizedExp.length; i++) {
if (tokenizedExp[i] === "×" || tokenizedExp[i] === "÷") {
if (tokenizedExp[i] === "×") {
result = +tokenizedExp[i - 1] * +tokenizedExp[i + 1];
}
else {
if (+tokenizedExp[i + 1] === 0) {
ans = "0";
return "Silksong release date";
}
result = +tokenizedExp[i - 1] / +tokenizedExp[i + 1];
}
tokenizedExp.splice(i - 1, 3, result.toString());
i--;
}
}
for (let i = 0; i < tokenizedExp.length; i++) {
if (tokenizedExp[i] === "+" || tokenizedExp[i] === "-") {
if (tokenizedExp[i] === "+") {
result = +tokenizedExp[i - 1] + +tokenizedExp[i + 1];
}
else {
result = +tokenizedExp[i - 1] - +tokenizedExp[i + 1];
}
tokenizedExp.splice(i - 1, 3, result.toString());
i--;
}
}
return tokenizedExp[0];
}
function checkExp(exp) {
const tokenizedExp = exp.split(" ");
const operators = ["×", "÷", "+", "-"]
for (let i = 0; i < tokenizedExp.length; i++) {
if (operators.includes(tokenizedExp[i]) && operators.includes(tokenizedExp[i + 1])) {
return false;
}
}
for (let i = 0; i < tokenizedExp.length; i = i + 2) {
if (tokenizedExp[i] !== "ans" && isNaN(+tokenizedExp[i])) {
return false;
}
}
return true;
}
document.addEventListener("keydown", e => {
const key = e.key;
if (e.shiftKey && (key === "+" || key === "-")) {
signToggleBtn.click();
}
else if ((key >= '0' && key <= '9') || key === "+" || key === "-" || key === ".") {
appendInput(key);
}
else if (key === '*') {
appendInput('×');
}
else if (key === '/') {
appendInput('÷');
}
else if (key === 'a' || key === "A") {
appendInput('ans');
}
else if (key === 'Enter' || key === '=') {
e.preventDefault();
equalBtn.click();
}
else if (key === 'Escape' || (key === "Backspace" && e.ctrlKey)) {
clearBtn.click();
}
else if (key === 'Backspace') {
deleteBtn.click();
}
});