From b9230735408644a60969d39adc7a8a45bc6257d1 Mon Sep 17 00:00:00 2001 From: Smig Tech Date: Sun, 8 Oct 2023 11:14:17 -0400 Subject: [PATCH] feat: negate/percentage methods added --- calculator/index.html | 2 +- calculator/js/script.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/calculator/index.html b/calculator/index.html index 19c3434..9151946 100644 --- a/calculator/index.html +++ b/calculator/index.html @@ -16,7 +16,7 @@
- + diff --git a/calculator/js/script.js b/calculator/js/script.js index 32f26ff..6edbce6 100644 --- a/calculator/js/script.js +++ b/calculator/js/script.js @@ -92,6 +92,12 @@ function handleOperatorClick(operatorClicked) { return; } else if (operatorClicked === '=') { handleOperation(); + } else if (operatorClicked === '+/-') { + negateNumber(); + return + } else if (operatorClicked === '%') { + percentageNumber(); + return } else { if(ops.secondNum && ops.numFlag){ handleOperation(); @@ -121,6 +127,31 @@ function calculator(event){ } } +function negateNumber(){ + // returns a negated number + let negated; + if (!ops.numFlag || !ops.onSecondNumber) { + negated = Number(ops.firstNum) * -1; + ops.firstNum = String(negated); + } else { + negated = Number(ops.secondNum) * -1; + ops.secondNum = String(negated); + } + updateDisplay(negated); +} + +function percentageNumber() { + let percent; + if (!ops.numFlag || !ops.onSecondNumber) { + percent = Number(ops.firstNum) / 100; + ops.firstNum = String(percent); + } else { + percent = Number(ops.secondNum) / 100; + ops.secondNum = String(percent); + } + updateDisplay(percent); +} + buttons.forEach(btn => { btn.addEventListener('click', calculator); }); \ No newline at end of file