Skip to content

Commit b77f2e1

Browse files
committed
feat(JS) refactor toggleSign()
1 parent c7435e8 commit b77f2e1

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

index.js

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ btns.forEach((btn) => {
1414

1515
deleteEverythingFromScreen(buttonValue);
1616

17+
toggleSign(buttonValue);
18+
1719

1820
if (Number(buttonValue) === 0 && screen.innerText.startsWith("0.")) {
1921
screen.innerText += buttonValue;
@@ -36,9 +38,9 @@ btns.forEach((btn) => {
3638
data.push(buttonValue);
3739
screen.innerText = data.join("");
3840
}
39-
if (buttonValue === "minus") {
40-
toggleSign();
41-
}
41+
42+
43+
4244

4345

4446
canUserAddDot(buttonValue);
@@ -110,6 +112,9 @@ btns.forEach((btn) => {
110112
});
111113
});
112114

115+
// end of forEach() statement
116+
117+
113118
function deteLastEntry() {
114119
let newArray = data.slice(0, -1);
115120
screen.innerText = newArray.join("");
@@ -151,26 +156,30 @@ function deleteEverythingFromScreen(button) {
151156
}
152157
}
153158

154-
function toggleSign() {
155-
let currentExpression = data.join("");
156-
let reversedExpression = currentExpression.split("").reverse().join("");
157-
let match = reversedExpression.match(/(\d+(\.\d+)?)|(\D+)/); // Match a number or non-digit
158-
// debugger
159-
160-
if (match) {
161-
let start = currentExpression.length - match[0].length;
162-
let end = currentExpression.length;
163-
let currentValue = Number(match[0]);
164-
165-
if (!isNaN(currentValue)) {
166-
// If it's a number, toggle its sign
167-
currentValue = -currentValue;
168-
data = data
169-
.slice(0, start)
170-
.concat(currentValue.toString().split(""), data.slice(end));
171-
screen.innerText = data.join("");
159+
function toggleSign(button) {
160+
if(button === "minus") {
161+
let currentExpression = data.join("");
162+
let reversedExpression = currentExpression.split("").join("");
163+
let match = reversedExpression.match(/(\d+(\.\d+)?)|(\D+)/); // Match a number or non-digit
164+
// debugger
165+
166+
if (match) {
167+
let start = currentExpression.length - match[0].length;
168+
let end = currentExpression.length;
169+
let currentValue = Number(match[0]);
170+
171+
if (!isNaN(currentValue)) {
172+
// If it's a number, toggle its sign
173+
currentValue = -currentValue;
174+
data = data
175+
.slice(0, start)
176+
.concat(currentValue.toString().split(""), data.slice(end));
177+
screen.innerText = data.join("");
178+
}
172179
}
180+
173181
}
182+
174183
}
175184

176185
function insertOpeningParenthesis(button) {

0 commit comments

Comments
 (0)