Skip to content
This repository was archived by the owner on Oct 22, 2021. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions task.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand All @@ -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
Expand Down Expand Up @@ -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');
Expand All @@ -96,8 +110,9 @@ keys.addEventListener('click', (event) => {
}

if (target.classList.contains('all-clear')) {
resetCalculator();
resetCalculator();
updateDisplay();
//console.log('all-clear',target.value);
return;
}

Expand Down