Skip to content

Commit b4c7310

Browse files
committed
feat(JS): add modify toggleSign()
1 parent 1792340 commit b4c7310

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

index.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,25 @@ function deleteEverythingFromScreen() {
146146
}
147147

148148
function toggleSign() {
149-
let currentValue = data[data.length - 1];
150-
if (currentValue === undefined) return;
151-
let toggledValue = -currentValue;
152-
// screen.innerText = toggledValue; // Update the screen
153-
// Update the value in the data array
154-
data[data.length - 1] = toggledValue;
155-
screen.innerText = data.join("");
149+
let currentExpression = data.join("");
150+
let reversedExpression = currentExpression.split("").reverse().join("");
151+
let match = reversedExpression.match(/(\d+(\.\d+)?)|(\D+)/); // Match a number or non-digit
152+
// debugger
153+
154+
if (match) {
155+
let start = currentExpression.length - match[0].length;
156+
let end = currentExpression.length;
157+
let currentValue = Number(match[0]);
158+
159+
if (!isNaN(currentValue)) {
160+
// If it's a number, toggle its sign
161+
currentValue = -currentValue;
162+
data = data.slice(0, start).concat(currentValue.toString().split(""), data.slice(end));
163+
screen.innerText = data.join("");
164+
}
165+
}
156166
}
167+
168+
169+
170+

0 commit comments

Comments
 (0)