feat: negate/percentage methods added

This commit is contained in:
Mike 2023-10-08 11:14:17 -04:00
parent 9a5a6d6742
commit b923073540
2 changed files with 32 additions and 1 deletions

View file

@ -16,7 +16,7 @@
</div>
<div class="buttons">
<button id="clear" class="btn" value="cls" data-ops="">A/C</button>
<button id="action" class="btn" value="+/-" data-negate="">+/-</button>
<button id="action" class="btn" value="+/-" data-ops="">+/-</button>
<button id="action" class="btn" value="%" data-ops="">%</button>
<button id="action" class="btn" value="/" data-ops="">÷</button>
<button class="btn" value="7" data-num="">7</button>

View file

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