From 2aaaf9c2b40da5078ed90d62b47d4ed1f24682bd Mon Sep 17 00:00:00 2001 From: Alexandros Florides Date: Sat, 10 Jun 2023 12:40:47 +0300 Subject: [PATCH 1/2] Fix multiple operators --- index.html | 2 +- scripts/script.js | 58 ++++++++++++++++++++++------------------------- 2 files changed, 28 insertions(+), 32 deletions(-) diff --git a/index.html b/index.html index 6088c850..737f4fd5 100644 --- a/index.html +++ b/index.html @@ -31,7 +31,7 @@

Calculator

- +
diff --git a/scripts/script.js b/scripts/script.js index 67931195..ea95109e 100644 --- a/scripts/script.js +++ b/scripts/script.js @@ -6,6 +6,9 @@ const themeIcon = document.getElementById("theme-icon"); const res = document.getElementById("result"); const toast = document.getElementById("toast"); +const operators = ["+", "-", "*", "/"] +let last_num = "" + function calculate(value) { const calculatedValue = eval(value || null); if (isNaN(calculatedValue)) { @@ -15,9 +18,15 @@ function calculate(value) { }, 1300); } else { res.value = calculatedValue; + last_num = calculatedValue.toString() } } +function clearValue(){ + result.value = ""; + last_num = ""; +} + // Swaps the stylesheet to achieve dark mode. function changeTheme() { const theme = document.getElementById("theme"); @@ -54,44 +63,30 @@ function keyboardInputHandler(e) { //grabbing the liveScreen //numbers - if (e.key === "0") { - res.value += "0"; - } else if (e.key === "1") { - res.value += "1"; - } else if (e.key === "2") { - res.value += "2"; - } else if (e.key === "3") { - res.value += "3"; - } else if (e.key === "4") { - res.value += "4"; - } else if (e.key === "5") { - res.value += "5"; - } else if (e.key === "6") { - res.value += "6"; - } else if (e.key === "7") { - res.value += "7"; - } else if (e.key === "7") { - res.value += "7"; - } else if (e.key === "8") { - res.value += "8"; - } else if (e.key === "9") { - res.value += "9"; + if (isFinite(e.key)){ + res.value += e.key; + last_num += e.key } + var last_char = res.value[res.value.length-1] + //operators - if (e.key === "+") { - res.value += "+"; - } else if (e.key === "-") { - res.value += "-"; - } else if (e.key === "*") { - res.value += "*"; - } else if (e.key === "/") { - res.value += "/"; + if (operators.includes(e.key)){ + // replace last operator if already exists + if (operators.includes(last_char)){ + res.value = res.value.slice(0, -1) + } + last_num = "" + res.value += e.key; } //decimal key if (e.key === ".") { - res.value += "."; + // allow only one decimal + if (last_char !== e.key && !last_num.includes(e.key)){ + res.value += e.key; + last_num += e.key + } } //press enter to see result @@ -104,5 +99,6 @@ function keyboardInputHandler(e) { const resultInput = res.value; //remove the last element in the string res.value = resultInput.substring(0, res.value.length - 1); + last_num = last_num.slice(0, -1) } } From 9554ee37dc407a2f69e67c25b4e32d61064aae6a Mon Sep 17 00:00:00 2001 From: Alexandros Florides Date: Sat, 10 Jun 2023 12:53:47 +0300 Subject: [PATCH 2/2] fixes --- scripts/script.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/scripts/script.js b/scripts/script.js index ea95109e..fa810b89 100644 --- a/scripts/script.js +++ b/scripts/script.js @@ -49,7 +49,8 @@ function liveScreen(enteredValue) { if (!res.value) { res.value = ""; } - res.value += enteredValue; + keyboardInputHandler({key:enteredValue}) + //res.value += enteredValue; } //adding event handler on the document to handle keyboard inputs @@ -57,9 +58,12 @@ document.addEventListener("keydown", keyboardInputHandler); //function to handle keyboard inputs function keyboardInputHandler(e) { + // to fix the default behavior of browser, // enter and backspace were causing undesired behavior when some key was already in focus. - e.preventDefault(); + if (e.target){ + e.preventDefault(); + } //grabbing the liveScreen //numbers