-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
148 lines (128 loc) · 3.82 KB
/
Copy pathscript.js
File metadata and controls
148 lines (128 loc) · 3.82 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
const displayEl = document.getElementById("display");
const historyEl = document.getElementById("history");
const buttons = document.querySelectorAll(".calculator-buttons .btn");
let currentValue = "0";
let historyValue = "";
let justEvaluated = false;
function updateDisplay() {
displayEl.textContent = currentValue;
historyEl.textContent = historyValue;
}
function clearAll() {
currentValue = "0";
historyValue = "";
justEvaluated = false;
updateDisplay();
}
function deleteLast() {
if (justEvaluated) {
// after result, DEL should clear everything
clearAll();
return;
}
if (currentValue.length <= 1) {
currentValue = "0";
} else {
currentValue = currentValue.slice(0, -1);
}
updateDisplay();
}
function appendValue(value) {
if (justEvaluated && /[0-9.]/.test(value)) {
// start new number after evaluation
currentValue = value === "." ? "0." : value;
justEvaluated = false;
} else if (currentValue === "0" && value !== "." && !isNaN(value)) {
// replace leading 0 for numbers
currentValue = value;
} else {
// avoid multiple dots in one number chunk
if (value === ".") {
const parts = currentValue.split(/[\+\-\*\/%]/);
const lastPart = parts[parts.length - 1];
if (lastPart.includes(".")) return;
}
currentValue += value;
}
updateDisplay();
}
function togglePlusMinus() {
if (currentValue === "0") return;
// find last number segment
const match = currentValue.match(/(-?\d*\.?\d+)(?!.*\d)/);
if (!match) return;
const numberStr = match[1];
const startIndex = match.index;
const before = currentValue.slice(0, startIndex);
const after = currentValue.slice(startIndex + numberStr.length);
const toggled = numberStr.startsWith("-")
? numberStr.slice(1)
: "-" + numberStr;
currentValue = before + toggled + after;
updateDisplay();
}
function evaluateExpression() {
try {
// Use a safe-ish evaluation:
// allow only digits, +-*/%. and spaces
const safeExpr = currentValue.replace(/[^0-9+\-*/%.]/g, "");
if (!safeExpr) return;
const result = Function(`"use strict"; return (${safeExpr})`)();
if (result === undefined || result === null || isNaN(result)) {
return;
}
historyValue = currentValue + " =";
currentValue = String(result);
justEvaluated = true;
updateDisplay();
} catch (err) {
currentValue = "Error";
justEvaluated = true;
updateDisplay();
}
}
buttons.forEach((btn) => {
const value = btn.getAttribute("data-value");
const action = btn.getAttribute("data-action");
btn.addEventListener("click", () => {
if (action === "clear") {
clearAll();
return;
}
if (action === "delete") {
deleteLast();
return;
}
if (action === "plusminus") {
togglePlusMinus();
return;
}
if (action === "equals") {
evaluateExpression();
return;
}
if (value) {
appendValue(value);
}
});
});
// Optional: keyboard support
document.addEventListener("keydown", (e) => {
const key = e.key;
if (/\d/.test(key)) {
appendValue(key);
} else if (key === ".") {
appendValue(".");
} else if (["+", "-", "*", "/"].includes(key)) {
appendValue(key);
} else if (key === "Enter" || key === "=") {
e.preventDefault();
evaluateExpression();
} else if (key === "Backspace") {
deleteLast();
} else if (key.toLowerCase() === "c" || key === "Escape") {
clearAll();
}
});
// Initialize display on first load
updateDisplay();