-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
215 lines (156 loc) · 4.53 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
const operators = ["+", "-", "*", "/"];
const liveResultDiv = document.getElementById("liveResult");
const inputAreaDiv = document.getElementById("inputArea");
const inputButtons = document.querySelectorAll(".num-btn, .operator");
const calculateBtn = document.getElementById("calculateBtn");
const deleteBtn = document.getElementById("deleteBtn");
const clearAllBtn = document.getElementById("clearAllBtn");
let stack = [];
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a - b;
}
function multiply(a, b) {
return a * b;
}
function divide(a, b) {
return a / b;
}
function operate(a, b, operator) {
switch (operator) {
case "+":
return add(a, b)
case "-":
return subtract(a, b)
case "*":
return multiply(a, b)
case "/":
return divide(a, b)
default:
return "Unknown Operator"
}
}
inputButtons.forEach(function (element) {
element.addEventListener("click", function () {
handleInput(element.textContent);
});
});
calculateBtn.addEventListener("click", showResult);
deleteBtn.addEventListener("click", deleteNumber);
clearAllBtn.addEventListener("click", clearAll);
function handleInput(input) {
if (input === ".") {
if (!shouldAllowDot()) {
return false;
}
}
if (stack.length === 0) {
if (operators.includes(input) && input !== "-" && input !== "+") {
return false;
}
}
if (operators.includes(input) && operators.includes(stack.at(-1))) {
stack.pop();
}
if (!isNaN(input)) {
stack.push(parseFloat(input));
} else {
stack.push(input);
}
let lastResult = calculateResult(stack);
updateScreen(lastResult);
}
function calculateResult(arr) {
if (arr.length === 0) {
return 0;
}
if (!arr.some(value => operators.includes(value))) {
return parseFloat(arr.join(""));
}
let reversedArr = arr.slice().reverse();
let operatorIndex = arr.length - reversedArr.findIndex(value => operators.includes(value)) - 1;
let firstPart = calculateResult(arr.slice(0, operatorIndex));
if (operators.includes(reversedArr[0])) {
return firstPart;
}
let secondPart = parseFloat(arr.slice(operatorIndex + 1).join(""));
let currentOperator = arr[operatorIndex];
return operate(firstPart, secondPart, currentOperator);
}
function parseResult(result) {
if (result === 0 && !stack.some(value => operators.includes(value))) {
return "";
} else {
return result.toString();
}
}
function updateScreen(result) {
let inputText = stack.join("");
inputAreaDiv.textContent = inputText;
let resultText = parseResult(result);
if (!(inputText === resultText)) {
updateLiveResult(resultText);
}
}
function showResult() {
let finalResult = calculateResult(stack);
inputAreaDiv.textContent = finalResult;
updateLiveResult("");
addResultToStack(finalResult);
}
function addResultToStack(result) {
let numbers = result.toString().split("");
stack = [];
numbers.forEach(function (value) {
if (value === ".") {
stack.push(".");
} else {
stack.push(parseFloat(value));
}
});
}
function deleteNumber() {
stack.pop();
let lastResult = calculateResult(stack);
updateScreen(lastResult);
}
function clearAll() {
stack = [];
inputAreaDiv.textContent = "";
updateLiveResult("");
}
function shouldAllowDot() {
if (!stack.includes(".")) {
return true;
}
if (stack.at(-1) === ".") {
return false;
}
let lastOperatorIndex = stack.length - stack.slice().reverse().findIndex(value => operators.includes(value)) - 1;
return lastOperatorIndex === -1 || !stack.slice(lastOperatorIndex + 1).includes(".");
}
document.addEventListener("keydown", function (e) {
e.preventDefault();
if (e.key === " ") {
return false;
}
if (!isNaN(e.key) || operators.includes(e.key) || e.key === ".") {
handleInput(e.key);
} else if (e.key === "Backspace" || e.key === "Delete") {
deleteNumber();
} else if (e.key === "Enter") {
showResult();
}
return false;
});
function updateLiveResult(content) {
if (content === "NaN") {
liveResultDiv.textContent = "";
} else if (content === "Infinity") {
liveResultDiv.textContent = "Result is Infinity. Can you handle it?"
} else {
liveResultDiv.textContent = content;
}
}