From 9a5a6d6742d776805336def15011224fe8854a8f Mon Sep 17 00:00:00 2001 From: Smig Tech Date: Sun, 8 Oct 2023 10:31:02 -0400 Subject: [PATCH] feat: implemented correct operations clear buttons work now --- calculator/css/style.css | 2 +- calculator/index.html | 2 +- calculator/js/script.js | 72 +++++++++++++++++----------------------- 3 files changed, 32 insertions(+), 44 deletions(-) diff --git a/calculator/css/style.css b/calculator/css/style.css index 5a53b5d..fe10efa 100644 --- a/calculator/css/style.css +++ b/calculator/css/style.css @@ -5,7 +5,7 @@ border-radius: 10px; } -.screen { +.display { height: 100px; background-color: #DFD3C3; margin: 10px 18px 15px; diff --git a/calculator/index.html b/calculator/index.html index f640283..19c3434 100644 --- a/calculator/index.html +++ b/calculator/index.html @@ -11,7 +11,7 @@
-
+
0
diff --git a/calculator/js/script.js b/calculator/js/script.js index 0c0efab..32f26ff 100644 --- a/calculator/js/script.js +++ b/calculator/js/script.js @@ -1,4 +1,4 @@ -let screen = document.querySelector('.screen'); +let display = document.querySelector('.display'); const buttons = document.querySelectorAll('.btn'); const ops = { @@ -6,6 +6,7 @@ const ops = { secondNum: String(), operator: undefined, numFlag: false, + onSecondNumber: false, updateNum: (num) => { if (!this.updateSecondNum){ @@ -22,7 +23,8 @@ const ops = { this.secondNum = String(); this.operator = undefined; this.numFlag = false; - screen.textContent = "0"; + this.onSecondNumber = false; + display.textContent = "0"; } }; @@ -38,17 +40,17 @@ function multiply(x, y) { return x * y; } -function divive(x, y) { +function divede(x, y) { if (x === 0) { return 0 } else if (y === 0) { alert("Cannot divide by 0!"); + return "Error" } return x / y; } function operate(firstNum, secondNum, operator) { - console.log(operator); switch (operator) { case '+': return add(firstNum, secondNum); @@ -57,45 +59,50 @@ function operate(firstNum, secondNum, operator) { case '*': return multiply(firstNum, secondNum); case '/': - return divive(firstNum, secondNum); + return divede(firstNum, secondNum); } } function handleOperation() { - first = Number(ops.firstNum); - second = Number(ops.secondNum); + let first = Number(ops.firstNum); + let second = Number(ops.secondNum); let result = operate(first, second, ops.operator); + result = Math.round(result * (10^3)/(10^3)); updateDisplay(result); - ops.operator = undefined; ops.firstNum = result; ops.secondNum = String(); - ops.numFlag = false; + if (ops.operator === '=') { + ops.numFlag = false; + ops.onSecondNumber = false; + } else { + ops.onSecondNumber = true; + } + ops.operator = undefined; } function updateDisplay(displayValue) { - screen.textContent = displayValue; + display.textContent = displayValue; } function handleOperatorClick(operatorClicked) { // TODO handle ops for = or clear - if (operatorClicked === '=' || operatorClicked === 'cls') { - if (operatorClicked === 'cls') { - ops.clear(); - } - if (operatorClicked === '=') { + if (operatorClicked === 'cls') { + ops.clear(); + return; + } else if (operatorClicked === '=') { + handleOperation(); + } else { + if(ops.secondNum && ops.numFlag){ handleOperation(); } } ops.operator = operatorClicked; //TODO fix this conditional should be if numFlag and a check if = or additional ops - if(ops.secondNum && ops.numFlag){ - handleOperation(); - console.log("this should be second num doing stuff"); - } else { - ops.toggleNumFlag(); - console.log('set second num'); - } + if(!ops.onSecondNumber){ + ops.numFlag = true; + } + ops.onSecondNumber = !ops.onSecondNumber; } function calculator(event){ @@ -112,27 +119,8 @@ function calculator(event){ updateDisplay(ops.secondNum); } } - - // if (currentScreen.length >= 8) return; - } - - buttons.forEach(btn => { btn.addEventListener('click', calculator); -}); - - -/* - -user imputs 1stNumber up to 8 digits or until user presses operator -number should be stored. each input evauluating for operator -1st number constantly updating -operator stored -user then enters 2nd Number up to 8 digits or until operator pressed -if user presses equals result is stored -if user presses operator result is saved to 1st number - - -*/ \ No newline at end of file +}); \ No newline at end of file