From 65ce64f08e7834f936effad8b63a5ec732512ea4 Mon Sep 17 00:00:00 2001 From: NetMan <13informatyka14@gmail.com> Date: Thu, 18 Jan 2024 20:12:58 +0100 Subject: [PATCH] Finished for now - more information in the body! New behaviour for 0 division, now multiple operations can be executed without pressing equals every operation, floating math reduced to 5, added using checks for numbers --- script.js | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/script.js b/script.js index bac110b..2fca93a 100644 --- a/script.js +++ b/script.js @@ -15,6 +15,9 @@ function multiply(a, b) { } function divide(a, b) { + if (b == 0) { + return "Dividing by 0? That's illegal!"; + } return a / b; } @@ -113,6 +116,11 @@ function handleOperators(value) { if (operation.addToA) { operation.operator = value; } + if (operation.readyForEquasion) { + execute(operation); + operation.addToA = !operation.addToA; + operation.operator = value; + } operation.outputOfEquasion = false; } @@ -135,12 +143,20 @@ function clear() { valueGui.textContent = operation.a; } -function populateDisplay(operation) { +function display(operation) { signGui.textContent = `${operation.operator}`; if (operation.displayA) { - valueGui.textContent = `${operation.a}`; - } else { - valueGui.textContent = `${operation.b}`; + if (operation.a[operation.a.length - 1] == ".") { + valueGui.textContent = `${operation.a}`; + } else { + valueGui.textContent = `${Math.round(parseFloat(operation.a) * 100000) / 100000}`; + } + } else { + if (operation.b[operation.b.length - 1] == ".") { + valueGui.textContent = `${operation.b}`; + } else { + valueGui.textContent = `${Math.round(parseFloat(operation.b) * 100000) / 100000}`; + } } } @@ -154,6 +170,9 @@ function handleAction(e) { } let action = true; let passLetter = e.key.toLowerCase(); + if (!checkNumber(operation.a) || !checkNumber(operation.b)) { + clear(); + } if (passLetter == " " || passLetter == ".") { addPoint(); @@ -177,7 +196,8 @@ function handleAction(e) { action = false; } if (action) { - populateDisplay(operation); + display(operation); + e.preventDefault(); }; }