mirror of
https://gitea.smigz.com/smiggiddy/odin-codeprojects.git
synced 2024-12-25 22:10:43 -05:00
feat: works for one operation
This commit is contained in:
parent
67f5d765f5
commit
32524f150e
2 changed files with 26 additions and 7 deletions
|
@ -15,10 +15,10 @@
|
|||
0
|
||||
</div>
|
||||
<div class="buttons">
|
||||
<button id="clear" class="btn" value="cls" data-clear="">A/C</button>
|
||||
<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>
|
||||
<button class="btn" value="8" data-num="">8</button>
|
||||
<button class="btn" value="9" data-num="">9</button>
|
||||
|
|
|
@ -18,10 +18,11 @@ const ops = {
|
|||
this.numFlag = !this.numFlag
|
||||
},
|
||||
clear: function(){
|
||||
this.firstNum = Number();
|
||||
this.secondNum = Number();
|
||||
this.firstNum = String();
|
||||
this.secondNum = String();
|
||||
this.operator = undefined;
|
||||
this.numFlag = false;
|
||||
screen.textContent = "0";
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -47,6 +48,7 @@ function divive(x, y) {
|
|||
}
|
||||
|
||||
function operate(firstNum, secondNum, operator) {
|
||||
console.log(operator);
|
||||
switch (operator) {
|
||||
case '+':
|
||||
return add(firstNum, secondNum);
|
||||
|
@ -59,6 +61,17 @@ function operate(firstNum, secondNum, operator) {
|
|||
}
|
||||
}
|
||||
|
||||
function handleOperation() {
|
||||
first = Number(ops.firstNum);
|
||||
second = Number(ops.secondNum);
|
||||
let result = operate(first, second, ops.operator);
|
||||
updateDisplay(result);
|
||||
ops.operator = undefined;
|
||||
ops.firstNum = result;
|
||||
ops.secondNum = String();
|
||||
ops.numFlag = false;
|
||||
}
|
||||
|
||||
function updateDisplay(displayValue) {
|
||||
screen.textContent = displayValue;
|
||||
}
|
||||
|
@ -67,12 +80,18 @@ function handleOperatorClick(operatorClicked) {
|
|||
|
||||
// TODO handle ops for = or clear
|
||||
if (operatorClicked === '=' || operatorClicked === 'cls') {
|
||||
console.log('Clear or = pressed');
|
||||
if (operatorClicked === 'cls') {
|
||||
ops.clear();
|
||||
}
|
||||
if (operatorClicked === '=') {
|
||||
handleOperation();
|
||||
}
|
||||
}
|
||||
ops.operator = operatorClicked;
|
||||
//TODO fix this conditional should be if numFlag and a check if = or additional ops
|
||||
if(ops.secondNum){
|
||||
console.log(operate(ops.firstNum, ops.secondNum, ops.operator));
|
||||
if(ops.secondNum && ops.numFlag){
|
||||
handleOperation();
|
||||
console.log("this should be second num doing stuff");
|
||||
} else {
|
||||
ops.toggleNumFlag();
|
||||
console.log('set second num');
|
||||
|
|
Loading…
Reference in a new issue