Skip to content

Commit 61b2a1c

Browse files
committed
feat(JS) refactor code by creating toggleSign() and pass it in if statement with condition (buttonValue === minus)
1 parent e6924be commit 61b2a1c

File tree

1 file changed

+20
-13
lines changed

1 file changed

+20
-13
lines changed

index.js

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,7 @@ btns.forEach((btn) => {
5050
deleteEverythingFromScreen();
5151
}
5252

53-
function deleteEverythingFromScreen() {
54-
screen.innerText = "";
55-
data = [];
56-
screen.innerText = 0;
57-
}
53+
5854

5955
if (Number(buttonValue) === 0 && screen.innerText.startsWith("0.")) {
6056
screen.innerText += buttonValue;
@@ -66,20 +62,13 @@ btns.forEach((btn) => {
6662
}
6763
if (/[\/*\-+]/.test(buttonValue)) {
6864
if (data.slice(-1)[0] === ".") {
69-
console.debug(". removed");
7065
data.pop();
7166
}
7267
data.push(buttonValue);
7368
screen.innerText = data.join("");
7469
}
7570
if (buttonValue === "minus") {
76-
let currentValue = data[data.length - 1];
77-
if (currentValue === undefined) return;
78-
let toggledValue = -currentValue;
79-
screen.innerText = toggledValue; // Update the screen
80-
// Update the value in the data array
81-
data[data.length - 1] = toggledValue;
82-
console.log(data);
71+
toggleSign();
8372
}
8473

8574
if (buttonValue === ".") {
@@ -146,3 +135,21 @@ function canUserAddDot() {
146135
}
147136
screen.innerText = data.join("");
148137
}
138+
139+
function deleteEverythingFromScreen() {
140+
screen.innerText = "";
141+
data = [];
142+
screen.innerText = 0;
143+
}
144+
145+
function toggleSign() {
146+
let currentValue = data[data.length - 1];
147+
if (currentValue === undefined) return;
148+
let toggledValue = -currentValue;
149+
screen.innerText = toggledValue; // Update the screen
150+
// Update the value in the data array
151+
data[data.length - 1] = toggledValue;
152+
console.log(data);
153+
}
154+
155+

0 commit comments

Comments
 (0)