From 325f766f3db73db353cff2dadcec428f7ff8d72b Mon Sep 17 00:00:00 2001 From: FShinoda Date: Tue, 4 Jun 2024 21:57:31 -0300 Subject: [PATCH] feat: block operations out of main scructure - example: 2+=, 2=, 0.= and 4+ are out of main structure (number_operator number). These are blocked in this feature. --- index.js | 55 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/index.js b/index.js index 72d85f5..ddae185 100644 --- a/index.js +++ b/index.js @@ -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){