Fix sign change toggle
This commit is contained in:
parent
0f23a7d239
commit
97c9db13b9
1 changed files with 17 additions and 8 deletions
21
script.js
21
script.js
|
|
@ -97,18 +97,25 @@ function clear() {
|
||||||
|
|
||||||
|
|
||||||
function changeSign() {
|
function changeSign() {
|
||||||
let num = +mainScreen.textContent;
|
let num = mainScreen.textContent;
|
||||||
|
|
||||||
if (num > 0) {
|
if (num === "0") {
|
||||||
mainScreen.textContent = `${-num}`;
|
// Replace 0 with the negative sign
|
||||||
} else if (num < 0) {
|
|
||||||
mainScreen.textContent = `${num}`;
|
|
||||||
} else {
|
|
||||||
mainScreen.textContent = "-";
|
mainScreen.textContent = "-";
|
||||||
|
} else if (num === "-") {
|
||||||
|
// If already negative, reset to 0
|
||||||
|
mainScreen.textContent = "0";
|
||||||
|
} else if (num.startsWith("-")) {
|
||||||
|
// If the number is negative, remove the negative sign
|
||||||
|
mainScreen.textContent = num.substring(1);
|
||||||
|
} else {
|
||||||
|
// Toggle the sign for positive numbers
|
||||||
|
mainScreen.textContent = `-${num}`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function addDecimals(a, b) {
|
function addDecimals(a, b) {
|
||||||
a = String(a);
|
a = String(a);
|
||||||
b = String(b);
|
b = String(b);
|
||||||
|
|
@ -203,5 +210,7 @@ function handleKeys(e) {
|
||||||
} else if (e.key === "=" || e.key === "Enter") {
|
} else if (e.key === "=" || e.key === "Enter") {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
document.querySelector(".equal").click();
|
document.querySelector(".equal").click();
|
||||||
|
} else if (e.key === "_") {
|
||||||
|
document.querySelector(".plus-minus").click();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in a new issue