diff --git a/script.js b/script.js index 27a177c..432e157 100644 --- a/script.js +++ b/script.js @@ -31,7 +31,6 @@ function displayEval() { result = calc.calculate(mainScreen.textContent); } else { topScreen.textContent += mainScreen.textContent; - console.log("main: " + mainScreen.textContent); result = calc.calculate(topScreen.textContent); } @@ -46,7 +45,6 @@ function displayOperator() { let operator = this.textContent; if (operator == "-" && (isNaN(mainScreen.textContent) || mainScreen.textContent == "0")) { - console.log("Entering changeSign"); changeSign(); return; } @@ -89,11 +87,10 @@ function clear() { topScreen.textContent = ""; } else if (this.classList.contains("error") && mainScreen.textContent !== "0") { - mainScreen.textContent = mainScreen.textContent.trimEnd() - mainScreen.textContent = mainScreen.textContent.slice(0, -1); - - if (!isNaN(mainScreen.textContent.charAt(-2))) { - mainScreen.textContent = mainScreen.textContent.trimEnd() + if (/^[0-9.]+$/.test(mainScreen.textContent)) { + mainScreen.textContent = mainScreen.textContent.slice(0, -1); + } else { + mainScreen.textContent = ""; } if (mainScreen.textContent === "") { @@ -107,16 +104,12 @@ function changeSign() { let num = mainScreen.textContent; if (num === "0") { - // Replace 0 with the negative sign 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}`; } } @@ -163,7 +156,6 @@ function divideDecimals(a, b) { function Calculator(str) { - console.log("Calculator has been called") isEval = true; this.methods = {