feat: rounding, decimal limit, refactors

This commit is contained in:
Mike 2023-10-12 17:17:14 -04:00
parent b923073540
commit 7248f314e1
2 changed files with 28 additions and 11 deletions

View file

@ -16,7 +16,7 @@
align-items: flex-end;
font-family: 'Orbitron', sans-serif;
font-weight: 500;
font-size: 4.5em;
font-size: 4.3em;
padding: 4px;
color: #000;
}

View file

@ -7,6 +7,8 @@ const ops = {
operator: undefined,
numFlag: false,
onSecondNumber: false,
stringLength: 8,
periodClicked: false,
updateNum: (num) => {
if (!this.updateSecondNum){
@ -25,6 +27,20 @@ const ops = {
this.numFlag = false;
this.onSecondNumber = false;
display.textContent = "0";
},
appendNumber: function(num) {
if(!ops.numFlag && ops.checkLength(ops.firstNum)) {
ops.firstNum += num;
updateDisplay(ops.firstNum);
} else if(this.numFlag) {
if (this.checkLength(ops.secondNum)) {
ops.secondNum += num;
updateDisplay(ops.secondNum);
}
}
},
checkLength: function(string) {
return string.length < this.stringLength;
}
};
@ -67,7 +83,8 @@ function handleOperation() {
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));
// result = Math.round(result * (10^3)/(10^3));
result = result.toPrecision(3);
updateDisplay(result);
ops.firstNum = result;
ops.secondNum = String();
@ -115,15 +132,15 @@ function calculator(event){
if(event.target.dataset.ops === "") {
handleOperatorClick(event.target.value);
}
else if(event.target.dataset.num === "") {
if(!ops.numFlag) {
ops.firstNum += event.target.value;
updateDisplay(ops.firstNum);
} else if (event.target.value === '.') {
if (display.textContent.includes('.')) {
return
} else {
ops.secondNum += event.target.value;
updateDisplay(ops.secondNum);
ops.appendNumber(event.target.value)
}
} else if(event.target.dataset.num === "") {
ops.appendNumber(event.target.value)
}
}