Skip to content

Commit

Permalink
feat: block operations out of main scructure
Browse files Browse the repository at this point in the history
- example: 2+=, 2=, 0.= and 4+ are out of main structure (number_operator number). These are blocked in this feature.
  • Loading branch information
FShinoda committed Jun 5, 2024
1 parent 1e4d797 commit 325f766
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,48 @@ function fixDecimalPlaces(total){
}
};

function verifyOperationStructure(leftNumber, rightNumber){
if(leftNumber && rightNumber && currentOperator){
return true;
}

return false;
}

function operate(){
let operation, leftNumber, rightNumber, total;

operation = displayValue.split(currentOperator);
leftNumber = operation[0];
rightNumber = operation[1];
console.log(leftNumber, rightNumber, currentOperator);

switch(currentOperator){
case "+":
total = fixDecimalPlaces(add(leftNumber, rightNumber));
break;
case "-":
total = fixDecimalPlaces(subtract(leftNumber, rightNumber));
break;
case "x":
total = fixDecimalPlaces(multiply(leftNumber, rightNumber));
break;
case "/":
if(rightNumber == 0){
total = "!DIV";
if (verifyOperationStructure(leftNumber, rightNumber)){
switch(currentOperator){
case "+":
total = fixDecimalPlaces(add(leftNumber, rightNumber));
break;
};
total = fixDecimalPlaces(divide(leftNumber, rightNumber));
break;
default:
total = "probably an error";
case "-":
total = fixDecimalPlaces(subtract(leftNumber, rightNumber));
break;
case "x":
total = fixDecimalPlaces(multiply(leftNumber, rightNumber));
break;
case "/":
if(rightNumber == 0){
total = "!DIV";
break;
};
total = fixDecimalPlaces(divide(leftNumber, rightNumber));
break;
default:
total = "probably an error";
}

changeDisplay(total);
currentOperator = '';
rightNumber = '';
leftNumber = '';
}

changeDisplay(total);
currentOperator = '';
};

function addNumber(number){
Expand Down

0 comments on commit 325f766

Please sign in to comment.