From c75625be29870f37ede68a016e71d6e6aff4de1e Mon Sep 17 00:00:00 2001 From: Smig Tech Date: Wed, 4 Oct 2023 21:52:07 -0400 Subject: [PATCH] feat: updates 1st/2nd nums --- calculator/index.html | 38 +++++++++--------- calculator/js/script.js | 87 +++++++++++++++++++++++++++++++++-------- 2 files changed, 89 insertions(+), 36 deletions(-) diff --git a/calculator/index.html b/calculator/index.html index 3db8834..5833858 100644 --- a/calculator/index.html +++ b/calculator/index.html @@ -15,25 +15,25 @@ 0
- - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + +
diff --git a/calculator/js/script.js b/calculator/js/script.js index ed6d779..b8c14e5 100644 --- a/calculator/js/script.js +++ b/calculator/js/script.js @@ -1,13 +1,32 @@ -let firstNum = undefined; -let secondNum = undefined; -let operator = undefined; let screen = document.querySelector('.screen'); - - const buttons = document.querySelectorAll('.btn'); +const ops = { + firstNum: String(), + secondNum: String(), + operator: undefined, + numFlag: false, + + updateNum: (num) => { + if (!this.updateSecondNum){ + this.firstNum = num; + } else { + this.secondNum = num; + } + }, + toggleNumFlag: function() { + this.numFlag = !this.numFlag + }, + clear: function(){ + this.firstNum = Number(); + this.secondNum = Number(); + this.operator = undefined; + this.numFlag = false; + } +}; + function add(x, y) { - return Number(x + y); + return x + y; } function subtract(x, y) { @@ -41,21 +60,55 @@ function operate(firstNum, secondNum, operator) { } function updateDisplay(displayValue) { - let currentScreen = screen.textContent.trim(); - console.log(currentScreen); - if (currentScreen.length >= 8) return; + screen.textContent = displayValue; +} - if (Number(currentScreen) === 0) { - screen.textContent = displayValue; - } else { - screen.textContent += displayValue; +function calculator(event){ + + if(event.target.dataset.ops === "") { + // TODO handle ops for = or clear + // TODO other ops should indicate a swap to second number + ops.operator = event.target.value; + //TODO fix this conditional should be if numFlag and a check if = or additional ops + if(ops.secondNum){ + console.log(operate(ops.firstNum, ops.secondNum, ops.operator)); + console.log(ops.operator); + } else { + ops.toggleNumFlag(); + console.log(ops.numFlag); + console.log('set second num'); + } + } + else if(event.target.dataset.num === "") { + if(!ops.numFlag) { + ops.firstNum += event.target.value; + updateDisplay(ops.firstNum); + } else { + ops.secondNum += event.target.value; + updateDisplay(ops.secondNum); + } } + + // if (currentScreen.length >= 8) return; + } buttons.forEach(btn => { - btn.addEventListener('click', e => { - updateDisplay(e.target.value); - }); -}); \ No newline at end of file + 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