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(); }; }