mirror of
https://gitea.smigz.com/smiggiddy/odin-codeprojects.git
synced 2024-12-26 06:20:42 -05:00
Merge pull request #1 from smiggiddy/patch-calculator
merge: patch calculator
This commit is contained in:
commit
1ad3c8efdb
1 changed files with 32 additions and 20 deletions
|
@ -5,18 +5,13 @@ const ops = {
|
||||||
firstNum: String(),
|
firstNum: String(),
|
||||||
secondNum: String(),
|
secondNum: String(),
|
||||||
operator: undefined,
|
operator: undefined,
|
||||||
|
lastOperator: undefined,
|
||||||
numFlag: false,
|
numFlag: false,
|
||||||
onSecondNumber: false,
|
onSecondNumber: false,
|
||||||
stringLength: 8,
|
stringLength: 8,
|
||||||
periodClicked: false,
|
periodClicked: false,
|
||||||
|
result: String(),
|
||||||
|
|
||||||
updateNum: (num) => {
|
|
||||||
if (!this.updateSecondNum){
|
|
||||||
this.firstNum = num;
|
|
||||||
} else {
|
|
||||||
this.secondNum = num;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
toggleNumFlag: function() {
|
toggleNumFlag: function() {
|
||||||
this.numFlag = !this.numFlag
|
this.numFlag = !this.numFlag
|
||||||
},
|
},
|
||||||
|
@ -26,6 +21,7 @@ const ops = {
|
||||||
this.operator = undefined;
|
this.operator = undefined;
|
||||||
this.numFlag = false;
|
this.numFlag = false;
|
||||||
this.onSecondNumber = false;
|
this.onSecondNumber = false;
|
||||||
|
this.result = String();
|
||||||
display.textContent = "0";
|
display.textContent = "0";
|
||||||
},
|
},
|
||||||
appendNumber: function(num) {
|
appendNumber: function(num) {
|
||||||
|
@ -81,25 +77,32 @@ function operate(firstNum, secondNum, operator) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleOperation() {
|
function handleOperation() {
|
||||||
let first = Number(ops.firstNum);
|
const first = Number(ops.firstNum);
|
||||||
let second = Number(ops.secondNum);
|
const 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));
|
|
||||||
if (result === "error") {
|
if (result === "error") {
|
||||||
ops.clear();
|
ops.clear();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = roundThreeDecimals(result);
|
result = roundThreeDecimals(result);
|
||||||
updateDisplay(result);
|
updateDisplay(result);
|
||||||
ops.firstNum = result;
|
|
||||||
ops.secondNum = String();
|
// store result
|
||||||
if (ops.operator === '=') {
|
ops.result = result;
|
||||||
|
|
||||||
|
if (ops.lastOperator === '=') {
|
||||||
|
ops.firstNum = String();
|
||||||
|
ops.secondNum = String();
|
||||||
ops.numFlag = false;
|
ops.numFlag = false;
|
||||||
ops.onSecondNumber = false;
|
ops.onSecondNumber = !ops.onSecondNumber;
|
||||||
} else {
|
} else {
|
||||||
|
ops.firstNum = ops.result;
|
||||||
|
ops.secondNum = "";
|
||||||
ops.onSecondNumber = true;
|
ops.onSecondNumber = true;
|
||||||
}
|
}
|
||||||
ops.operator = undefined;
|
// ops.operator = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateDisplay(displayValue) {
|
function updateDisplay(displayValue) {
|
||||||
|
@ -116,29 +119,38 @@ function roundThreeDecimals(number) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleOperatorClick(operatorClicked) {
|
function handleOperatorClick(operatorClicked) {
|
||||||
|
|
||||||
|
ops.lastOperator = operatorClicked;
|
||||||
// TODO handle ops for = or clear
|
// TODO handle ops for = or clear
|
||||||
if (operatorClicked === 'cls') {
|
if (operatorClicked === 'cls') {
|
||||||
ops.clear();
|
ops.clear();
|
||||||
return;
|
return;
|
||||||
} else if (operatorClicked === '=') {
|
} else if (operatorClicked === '=') {
|
||||||
|
ops.lastOperator = '=';
|
||||||
handleOperation();
|
handleOperation();
|
||||||
|
return;
|
||||||
} else if (operatorClicked === '+/-') {
|
} else if (operatorClicked === '+/-') {
|
||||||
negateNumber();
|
negateNumber();
|
||||||
return
|
return;
|
||||||
} else if (operatorClicked === '%') {
|
} else if (operatorClicked === '%') {
|
||||||
percentageNumber();
|
percentageNumber();
|
||||||
return
|
return;
|
||||||
} else {
|
} else {
|
||||||
if(ops.secondNum && ops.numFlag){
|
if(ops.secondNum && ops.numFlag){
|
||||||
handleOperation();
|
handleOperation();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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.onSecondNumber){
|
if(!ops.onSecondNumber ){
|
||||||
ops.numFlag = true;
|
ops.numFlag = true;
|
||||||
}
|
} else if (ops.result !== undefined ) {
|
||||||
|
console.log('this is running the ops.result');
|
||||||
|
ops.firstNum = ops.result;
|
||||||
|
ops.onSecondNumber = true;
|
||||||
|
return
|
||||||
|
}
|
||||||
ops.onSecondNumber = !ops.onSecondNumber;
|
ops.onSecondNumber = !ops.onSecondNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue