-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
355 lines (305 loc) · 8.63 KB
/
script.js
File metadata and controls
355 lines (305 loc) · 8.63 KB
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
345
346
347
348
349
350
351
352
353
354
355
// State
let currentInput = "0";
let expression = [];
let history = [];
let shouldResetScreen = false;
let isResultDisplayed = false;
// DOM Elements
const mainDisplay = document.getElementById("main-display");
const expressionDisplay = document.getElementById("expression-display");
const keypad = document.getElementById("keypad");
const historyBtn = document.getElementById("history-btn");
const historyPanel = document.getElementById("history-panel");
const historyList = document.getElementById("history-list");
const clearHistoryBtn = document.getElementById("clear-history");
// Constants
const MAX_DIGITS = 12;
// Event Listeners
keypad.addEventListener("click", handleButtonClick);
window.addEventListener("keydown", handleKeyboardInput);
historyBtn.addEventListener("click", toggleHistory);
clearHistoryBtn.addEventListener("click", clearHistory);
// Close history when clicking outside
document.addEventListener("click", (e) => {
if (!historyPanel.contains(e.target) && !historyBtn.contains(e.target)) {
historyPanel.classList.add("hidden");
}
});
// Initial Display
updateDisplay();
function handleButtonClick(e) {
const button = e.target.closest("button");
if (!button) return;
const action = button.dataset.action;
const value = button.dataset.value;
processInput(action, value);
}
function handleKeyboardInput(e) {
const key = e.key;
if (key >= "0" && key <= "9") {
processInput("number", key);
} else if (key === ".") {
processInput("decimal");
} else if (key === "=" || key === "Enter") {
processInput("calculate");
} else if (key === "Backspace") {
processInput("delete"); // We can add a backspace feature or treat as C
} else if (key === "Escape") {
processInput("clear");
} else if (key === "+" || key === "-" || key === "*" || key === "/") {
processInput("operator", key);
} else if (key === "%") {
processInput("percent");
}
}
function processInput(action, value) {
switch (action) {
case "number":
handleNumber(value);
break;
case "operator":
handleOperator(value);
break;
case "decimal":
handleDecimal();
break;
case "clear":
handleClear();
break;
case "calculate":
handleCalculate();
break;
case "negate":
handleNegate();
break;
case "percent":
handlePercent();
break;
case "delete":
handleBackspace();
break;
}
updateDisplay();
}
// Logic Functions
function handleNumber(num) {
if (currentInput === "Error") handleClear();
if (shouldResetScreen || isResultDisplayed) {
currentInput = num;
shouldResetScreen = false;
isResultDisplayed = false;
} else {
if (currentInput === "0") {
currentInput = num;
} else {
if (currentInput.replace(/[^0-9]/g, "").length < MAX_DIGITS) {
currentInput += num;
}
}
}
}
function handleDecimal() {
if (shouldResetScreen || isResultDisplayed) {
currentInput = "0.";
shouldResetScreen = false;
isResultDisplayed = false;
return;
}
if (!currentInput.includes(".")) {
currentInput += ".";
}
}
function handleOperator(op) {
if (currentInput === "Error") return;
if (isResultDisplayed) {
// Continue calculation with result
expression = [currentInput, op];
isResultDisplayed = false;
shouldResetScreen = true;
} else if (shouldResetScreen) {
// Just changing the operator if user hits + then *
expression[expression.length - 1] = op;
} else {
// Push current number and new operator
expression.push(currentInput);
expression.push(op);
shouldResetScreen = true;
}
}
function handleNegate() {
if (currentInput === "Error") return;
if (currentInput === "0") return;
if (currentInput.startsWith("-")) {
currentInput = currentInput.slice(1);
} else {
currentInput = "-" + currentInput;
}
}
function handlePercent() {
if (currentInput === "Error") return;
const value = parseFloat(currentInput);
currentInput = (value / 100).toString();
}
function handleClear() {
currentInput = "0";
expression = [];
shouldResetScreen = false;
isResultDisplayed = false;
}
function handleBackspace() {
if (shouldResetScreen || isResultDisplayed || currentInput === "Error") {
handleClear();
return;
}
if (currentInput.length > 1) {
currentInput = currentInput.slice(0, -1);
} else {
currentInput = "0";
}
}
function handleCalculate() {
if (isResultDisplayed || currentInput === "Error") return;
// Add final number to expression
expression.push(currentInput);
// Save full expression for history before clearing
const fullExpression = [...expression];
const result = evaluateExpression(expression);
// Add to history
addToHistory(fullExpression, formatResult(result));
currentInput = formatResult(result);
expression = []; // Clear expression after calculation
shouldResetScreen = true;
isResultDisplayed = true;
}
function addToHistory(expr, result) {
const expressionString = expr
.join(" ")
.replace(/\*/g, "×")
.replace(/\//g, "÷");
history.unshift({ expression: expressionString, result: result });
if (history.length > 10) history.pop(); // Keep last 10 items
renderHistory();
}
function toggleHistory() {
historyPanel.classList.toggle("hidden");
}
function clearHistory() {
history = [];
renderHistory();
}
function renderHistory() {
historyList.innerHTML = "";
if (history.length === 0) {
historyList.innerHTML =
'<div class="text-xs text-text-secondary text-center py-4">No history yet</div>';
return;
}
history.forEach((item) => {
const div = document.createElement("div");
div.className =
"flex flex-col p-2 hover:bg-gray-50 rounded-lg cursor-pointer transition-colors";
div.innerHTML = `
<div class="text-xs text-text-secondary text-right mb-1">${item.expression} =</div>
<div class="text-lg text-text-main font-medium text-right">${item.result}</div>
`;
div.addEventListener("click", () => {
currentInput = item.result;
expression = [];
shouldResetScreen = true;
isResultDisplayed = true;
updateDisplay();
historyPanel.classList.add("hidden");
});
historyList.appendChild(div);
});
}
function formatResult(num) {
if (!isFinite(num) || isNaN(num)) return "Error";
// Fix floating point issues (e.g. 0.1 + 0.2)
let result = parseFloat(num.toPrecision(12));
// Remove trailing zeros
return result.toString();
}
// Stack-based Evaluation
function evaluateExpression(tokens) {
if (tokens.length === 0) return 0;
const values = [];
const ops = [];
const precedence = {
"+": 1,
"-": 1,
"*": 2,
"/": 2,
};
for (let i = 0; i < tokens.length; i++) {
const token = tokens[i];
if (!isNaN(parseFloat(token))) {
values.push(parseFloat(token));
} else if (token in precedence) {
while (
ops.length > 0 &&
precedence[ops[ops.length - 1]] >= precedence[token]
) {
const op = ops.pop();
const b = values.pop();
const a = values.pop();
values.push(applyOp(op, a, b));
}
ops.push(token);
}
}
while (ops.length > 0) {
const op = ops.pop();
const b = values.pop();
const a = values.pop();
values.push(applyOp(op, a, b));
}
return values[0];
}
function applyOp(op, a, b) {
switch (op) {
case "+":
return a + b;
case "-":
return a - b;
case "*":
return a * b;
case "/":
if (b === 0) return NaN; // Will result in 'Error'
return a / b;
}
return 0;
}
// UI Update
function updateDisplay() {
// Format Main Display (current number)
const formattedInput = formatNumberForDisplay(currentInput);
mainDisplay.textContent = formattedInput;
// Format Expression Display
const expressionString = expression
.join(" ")
.replace(/\*/g, "×")
.replace(/\//g, "÷");
expressionDisplay.textContent = expressionString;
// Scale font size if number is too long (basic handling)
if (currentInput.length > 9) {
mainDisplay.classList.remove("text-6xl");
mainDisplay.classList.add("text-4xl");
} else {
mainDisplay.classList.remove("text-4xl");
mainDisplay.classList.add("text-6xl");
}
}
function formatNumberForDisplay(numStr) {
if (numStr === "Error") return "Error";
if (numStr.endsWith(".")) {
// Handle "123." case - parseint would lose the decimal
const parts = numStr.split(".");
return parseFloat(parts[0]).toLocaleString("en-US") + ".";
}
if (numStr.includes(".")) {
const parts = numStr.split(".");
return parseFloat(parts[0]).toLocaleString("en-US") + "." + parts[1];
}
return parseFloat(numStr).toLocaleString("en-US");
}