diff --git a/task.js b/task.js index afea252..ea31d16 100644 --- a/task.js +++ b/task.js @@ -7,10 +7,11 @@ const calculator = { //Manage Operators const performCalculation = { + + '/': (firstOperand, secondOperand) => firstOperand / secondOperand, + '*': (firstOperand, secondOperand) => firstOperand * secondOperand, '+': (firstOperand, secondOperand) => firstOperand + secondOperand, - '-': (firstOperand, secondOperand) => firstOperand - secondOperand, - '=': (firstOperand, secondOperand) => secondOperand }; @@ -20,17 +21,19 @@ function inputDigit(digit) { waitingForSecondOperand } = calculator; - if (waitingForSecondOperand === true) { - calculator.displayValue = digit; - calculator.waitingForSecondOperand = false; + if (waitingForSecondOperand === true) { // waiting to enter a second number + calculator.displayValue = digit; // if the calculator waiting to get a second number and user enter a second number, then second number is assign as the display value + calculator.waitingForSecondOperand = false; // now waitingForSecondOperand variable set to false since we do not need any numbers to enter } else { - calculator.displayValue = displayValue === '0' ? digit : displayValue + digit; + calculator.displayValue = displayValue === '0' ? digit : displayValue + digit; // displayValue = 0 and entered digit adding to 0 and display the second value. (in case first number not entered, else condtion get executed) } console.log(calculator); } function inputDecimal(dot) { + + if (calculator.waitingForSecondOperand===true)return; // preventing the decimal point appended to the displayValue (only if the user entered nothing after type a dot. ex: 100. ) // If the `displayValue` does not contain a decimal point if (!calculator.displayValue.includes(dot)) { // Append the decimal point @@ -72,6 +75,17 @@ function updateDisplay() { display.value = calculator.displayValue; } +function resetCalculator(){ + + calculator.displayValue = '0'; + calculator.firstOperand = null; + calculator.waitingForSecondOperand= null; + calculator.operator = null; + console.log(calculator); + + +} + updateDisplay(); const keys = document.querySelector('.calculator-keys'); @@ -96,8 +110,9 @@ keys.addEventListener('click', (event) => { } if (target.classList.contains('all-clear')) { - resetCalculator(); + resetCalculator(); updateDisplay(); + //console.log('all-clear',target.value); return; }