mirror of
https://gitea.smigz.com/smiggiddy/odin-codeprojects.git
synced 2024-12-26 06:20:42 -05:00
feat: implemented correct operations
clear buttons work now
This commit is contained in:
parent
32524f150e
commit
9a5a6d6742
3 changed files with 32 additions and 44 deletions
|
@ -5,7 +5,7 @@
|
|||
border-radius: 10px;
|
||||
}
|
||||
|
||||
.screen {
|
||||
.display {
|
||||
height: 100px;
|
||||
background-color: #DFD3C3;
|
||||
margin: 10px 18px 15px;
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="screen">
|
||||
<div class="display">
|
||||
0
|
||||
</div>
|
||||
<div class="buttons">
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
let screen = document.querySelector('.screen');
|
||||
let display = document.querySelector('.display');
|
||||
const buttons = document.querySelectorAll('.btn');
|
||||
|
||||
const ops = {
|
||||
|
@ -6,6 +6,7 @@ const ops = {
|
|||
secondNum: String(),
|
||||
operator: undefined,
|
||||
numFlag: false,
|
||||
onSecondNumber: false,
|
||||
|
||||
updateNum: (num) => {
|
||||
if (!this.updateSecondNum){
|
||||
|
@ -22,7 +23,8 @@ const ops = {
|
|||
this.secondNum = String();
|
||||
this.operator = undefined;
|
||||
this.numFlag = false;
|
||||
screen.textContent = "0";
|
||||
this.onSecondNumber = false;
|
||||
display.textContent = "0";
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -38,17 +40,17 @@ function multiply(x, y) {
|
|||
return x * y;
|
||||
}
|
||||
|
||||
function divive(x, y) {
|
||||
function divede(x, y) {
|
||||
if (x === 0) {
|
||||
return 0
|
||||
} else if (y === 0) {
|
||||
alert("Cannot divide by 0!");
|
||||
return "Error"
|
||||
}
|
||||
return x / y;
|
||||
}
|
||||
|
||||
function operate(firstNum, secondNum, operator) {
|
||||
console.log(operator);
|
||||
switch (operator) {
|
||||
case '+':
|
||||
return add(firstNum, secondNum);
|
||||
|
@ -57,45 +59,50 @@ function operate(firstNum, secondNum, operator) {
|
|||
case '*':
|
||||
return multiply(firstNum, secondNum);
|
||||
case '/':
|
||||
return divive(firstNum, secondNum);
|
||||
return divede(firstNum, secondNum);
|
||||
}
|
||||
}
|
||||
|
||||
function handleOperation() {
|
||||
first = Number(ops.firstNum);
|
||||
second = Number(ops.secondNum);
|
||||
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));
|
||||
updateDisplay(result);
|
||||
ops.operator = undefined;
|
||||
ops.firstNum = result;
|
||||
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) {
|
||||
screen.textContent = displayValue;
|
||||
display.textContent = displayValue;
|
||||
}
|
||||
|
||||
function handleOperatorClick(operatorClicked) {
|
||||
|
||||
// TODO handle ops for = or clear
|
||||
if (operatorClicked === '=' || operatorClicked === 'cls') {
|
||||
if (operatorClicked === 'cls') {
|
||||
ops.clear();
|
||||
}
|
||||
if (operatorClicked === '=') {
|
||||
if (operatorClicked === 'cls') {
|
||||
ops.clear();
|
||||
return;
|
||||
} else if (operatorClicked === '=') {
|
||||
handleOperation();
|
||||
} else {
|
||||
if(ops.secondNum && ops.numFlag){
|
||||
handleOperation();
|
||||
}
|
||||
}
|
||||
ops.operator = operatorClicked;
|
||||
//TODO fix this conditional should be if numFlag and a check if = or additional ops
|
||||
if(ops.secondNum && ops.numFlag){
|
||||
handleOperation();
|
||||
console.log("this should be second num doing stuff");
|
||||
} else {
|
||||
ops.toggleNumFlag();
|
||||
console.log('set second num');
|
||||
}
|
||||
if(!ops.onSecondNumber){
|
||||
ops.numFlag = true;
|
||||
}
|
||||
ops.onSecondNumber = !ops.onSecondNumber;
|
||||
}
|
||||
|
||||
function calculator(event){
|
||||
|
@ -112,27 +119,8 @@ function calculator(event){
|
|||
updateDisplay(ops.secondNum);
|
||||
}
|
||||
}
|
||||
|
||||
// if (currentScreen.length >= 8) return;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
buttons.forEach(btn => {
|
||||
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
|
||||
|
||||
|
||||
*/
|
||||
});
|
Loading…
Reference in a new issue