feat: implemented correct operations

clear buttons work now
This commit is contained in:
Mike 2023-10-08 10:31:02 -04:00
parent 32524f150e
commit 9a5a6d6742
3 changed files with 32 additions and 44 deletions

View file

@ -5,7 +5,7 @@
border-radius: 10px; border-radius: 10px;
} }
.screen { .display {
height: 100px; height: 100px;
background-color: #DFD3C3; background-color: #DFD3C3;
margin: 10px 18px 15px; margin: 10px 18px 15px;

View file

@ -11,7 +11,7 @@
</head> </head>
<body> <body>
<div class="container"> <div class="container">
<div class="screen"> <div class="display">
0 0
</div> </div>
<div class="buttons"> <div class="buttons">

View file

@ -1,4 +1,4 @@
let screen = document.querySelector('.screen'); let display = document.querySelector('.display');
const buttons = document.querySelectorAll('.btn'); const buttons = document.querySelectorAll('.btn');
const ops = { const ops = {
@ -6,6 +6,7 @@ const ops = {
secondNum: String(), secondNum: String(),
operator: undefined, operator: undefined,
numFlag: false, numFlag: false,
onSecondNumber: false,
updateNum: (num) => { updateNum: (num) => {
if (!this.updateSecondNum){ if (!this.updateSecondNum){
@ -22,7 +23,8 @@ const ops = {
this.secondNum = String(); this.secondNum = String();
this.operator = undefined; this.operator = undefined;
this.numFlag = false; this.numFlag = false;
screen.textContent = "0"; this.onSecondNumber = false;
display.textContent = "0";
} }
}; };
@ -38,17 +40,17 @@ function multiply(x, y) {
return x * y; return x * y;
} }
function divive(x, y) { function divede(x, y) {
if (x === 0) { if (x === 0) {
return 0 return 0
} else if (y === 0) { } else if (y === 0) {
alert("Cannot divide by 0!"); alert("Cannot divide by 0!");
return "Error"
} }
return x / y; return x / y;
} }
function operate(firstNum, secondNum, operator) { function operate(firstNum, secondNum, operator) {
console.log(operator);
switch (operator) { switch (operator) {
case '+': case '+':
return add(firstNum, secondNum); return add(firstNum, secondNum);
@ -57,45 +59,50 @@ function operate(firstNum, secondNum, operator) {
case '*': case '*':
return multiply(firstNum, secondNum); return multiply(firstNum, secondNum);
case '/': case '/':
return divive(firstNum, secondNum); return divede(firstNum, secondNum);
} }
} }
function handleOperation() { function handleOperation() {
first = Number(ops.firstNum); let first = Number(ops.firstNum);
second = Number(ops.secondNum); let second = Number(ops.secondNum);
let result = operate(first, second, ops.operator); let result = operate(first, second, ops.operator);
result = Math.round(result * (10^3)/(10^3));
updateDisplay(result); updateDisplay(result);
ops.operator = undefined;
ops.firstNum = result; ops.firstNum = result;
ops.secondNum = String(); ops.secondNum = String();
ops.numFlag = false; if (ops.operator === '=') {
ops.numFlag = false;
ops.onSecondNumber = false;
} else {
ops.onSecondNumber = true;
}
ops.operator = undefined;
} }
function updateDisplay(displayValue) { function updateDisplay(displayValue) {
screen.textContent = displayValue; display.textContent = displayValue;
} }
function handleOperatorClick(operatorClicked) { function handleOperatorClick(operatorClicked) {
// TODO handle ops for = or clear // TODO handle ops for = or clear
if (operatorClicked === '=' || operatorClicked === 'cls') { if (operatorClicked === 'cls') {
if (operatorClicked === 'cls') { ops.clear();
ops.clear(); return;
} } else if (operatorClicked === '=') {
if (operatorClicked === '=') { handleOperation();
} else {
if(ops.secondNum && ops.numFlag){
handleOperation(); handleOperation();
} }
} }
ops.operator = operatorClicked; ops.operator = operatorClicked;
//TODO fix this conditional should be if numFlag and a check if = or additional ops //TODO fix this conditional should be if numFlag and a check if = or additional ops
if(ops.secondNum && ops.numFlag){ if(!ops.onSecondNumber){
handleOperation(); ops.numFlag = true;
console.log("this should be second num doing stuff"); }
} else { ops.onSecondNumber = !ops.onSecondNumber;
ops.toggleNumFlag();
console.log('set second num');
}
} }
function calculator(event){ function calculator(event){
@ -112,27 +119,8 @@ function calculator(event){
updateDisplay(ops.secondNum); updateDisplay(ops.secondNum);
} }
} }
// if (currentScreen.length >= 8) return;
} }
buttons.forEach(btn => { buttons.forEach(btn => {
btn.addEventListener('click', calculator); 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
*/