From abb1370a11134d5f4d0e0d783f8ba3be7376afbe Mon Sep 17 00:00:00 2001 From: Smig Tech Date: Sat, 14 Oct 2023 06:58:55 -0400 Subject: [PATCH 1/2] fix: new number add works --- calculator/js/script.js | 42 +++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/calculator/js/script.js b/calculator/js/script.js index 069a743..16bebeb 100644 --- a/calculator/js/script.js +++ b/calculator/js/script.js @@ -5,18 +5,13 @@ const ops = { firstNum: String(), secondNum: String(), operator: undefined, + lastOperator: undefined, numFlag: false, onSecondNumber: false, stringLength: 8, periodClicked: false, + result: String(), - updateNum: (num) => { - if (!this.updateSecondNum){ - this.firstNum = num; - } else { - this.secondNum = num; - } - }, toggleNumFlag: function() { this.numFlag = !this.numFlag }, @@ -26,6 +21,7 @@ const ops = { this.operator = undefined; this.numFlag = false; this.onSecondNumber = false; + this.result = String(); display.textContent = "0"; }, appendNumber: function(num) { @@ -81,25 +77,35 @@ function operate(firstNum, secondNum, operator) { } function handleOperation() { - let first = Number(ops.firstNum); - let second = Number(ops.secondNum); + const first = Number(ops.firstNum); + const second = Number(ops.secondNum); let result = operate(first, second, ops.operator); - // result = Math.round(result * (10^3)/(10^3)); + if (result === "error") { ops.clear(); return; } + result = roundThreeDecimals(result); updateDisplay(result); - ops.firstNum = result; - ops.secondNum = String(); - if (ops.operator === '=') { + + // store result + ops.result = result; + + if (ops.lastOperator === '=') { + console.log('This ran'); + ops.firstNum = String(); + ops.secondNum = String(); ops.numFlag = false; - ops.onSecondNumber = false; + ops.onSecondNumber = !ops.onSecondNumber; + console.log(`Changing second number ${ops.onSecondNumber}`); } else { + console.log("this else ran"); + ops.firstNum = ops.result; + ops.secondNum = ""; ops.onSecondNumber = true; } - ops.operator = undefined; + // ops.operator = undefined; } function updateDisplay(displayValue) { @@ -116,13 +122,16 @@ function roundThreeDecimals(number) { } function handleOperatorClick(operatorClicked) { - + + ops.lastOperator = operatorClicked; // TODO handle ops for = or clear if (operatorClicked === 'cls') { ops.clear(); return; } else if (operatorClicked === '=') { + ops.lastOperator = '='; handleOperation(); + return; } else if (operatorClicked === '+/-') { negateNumber(); return @@ -139,6 +148,7 @@ function handleOperatorClick(operatorClicked) { if(!ops.onSecondNumber){ ops.numFlag = true; } + ops.onSecondNumber = !ops.onSecondNumber; } From aa4c22850299d23c955c941ca202d912770a17c5 Mon Sep 17 00:00:00 2001 From: Smig Tech Date: Sat, 14 Oct 2023 07:29:07 -0400 Subject: [PATCH 2/2] fix: corrected additional add issue --- calculator/js/script.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/calculator/js/script.js b/calculator/js/script.js index 16bebeb..9e6c1ec 100644 --- a/calculator/js/script.js +++ b/calculator/js/script.js @@ -93,14 +93,11 @@ function handleOperation() { ops.result = result; if (ops.lastOperator === '=') { - console.log('This ran'); ops.firstNum = String(); ops.secondNum = String(); ops.numFlag = false; ops.onSecondNumber = !ops.onSecondNumber; - console.log(`Changing second number ${ops.onSecondNumber}`); } else { - console.log("this else ran"); ops.firstNum = ops.result; ops.secondNum = ""; ops.onSecondNumber = true; @@ -134,21 +131,26 @@ function handleOperatorClick(operatorClicked) { return; } else if (operatorClicked === '+/-') { negateNumber(); - return + return; } else if (operatorClicked === '%') { percentageNumber(); - return + return; } else { if(ops.secondNum && ops.numFlag){ handleOperation(); + return; } } ops.operator = operatorClicked; //TODO fix this conditional should be if numFlag and a check if = or additional ops - if(!ops.onSecondNumber){ + if(!ops.onSecondNumber ){ ops.numFlag = true; - } - + } else if (ops.result !== undefined ) { + console.log('this is running the ops.result'); + ops.firstNum = ops.result; + ops.onSecondNumber = true; + return + } ops.onSecondNumber = !ops.onSecondNumber; }