-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
127 lines (93 loc) · 3.38 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const calculator ={
displayValue:'0',
firstOperand:null,
waitingForSecond:false,
operator:null
}
const performCalculations = {
'+':(firstOperand, secondOperand) => firstOperand + secondOperand,
'-':(firstOperand, secondOperand) => firstOperand - secondOperand,
'*':(firstOperand, secondOperand) => firstOperand * secondOperand,
'/':(firstOperand, secondOperand) => firstOperand / secondOperand,
'=': (firstOperand, secondOperand) => secondOperand
}
//resetting the calculator
function resetCalculator(){
calculator.displayValue = '0',
calculator.firstOperand = null,
calculator.waitingForSecond =false,
calculator.operator = null
}
function inputDigits(digit){
const {displayValue, waitingForSecond} = calculator;
//handling display of second value after operator
if(waitingForSecond === true){
calculator.displayValue = digit;
calculator.waitingForSecond = false;
}else{
calculator.displayValue = displayValue === '0' ? digit : displayValue + digit;
}
console.log(calculator)
}
function inputDecimal(dot){
if(calculator.waitingForSecond === true) return;
//else append a dot to the string
if (!calculator.displayValue.includes(dot)) {
// Append the decimal point
calculator.displayValue += dot;
}
}
//Handling operators
function inputOperators(nextOperator){
// case 1: when user enters first operator and hits a operator
const {firstOperand, operator, displayValue, waitingForSecond} = calculator;
const inputValue = parseFloat(displayValue);
//case 3: When a user enters two or more operators consecutively, i.e changes the operation
if(operator && waitingForSecond === true){
calculator.operator = nextOperator;
return;
}
if(calculator.firstOperand === null){
calculator.firstOperand = inputValue;
}else if(operator){ //at this point we already have a operator and firstOperand
//case 2: when user finishes the second operand and hits a key
const currentValue = firstOperand || 0;
const result = performCalculations[operator](currentValue,inputValue)
//now store this result to firstOperand for further calculations
calculator.displayValue = String(result)
calculator.firstOperand = result;
}
calculator.waitingForSecond = true;
calculator.operator = nextOperator;
}
function handleInputs(e){
const{target} = event; /* is equivalent to const target = event.target;*/
if(!target.matches('button')) return; // if clicked on div elsewhere
if(target.classList.contains('operator')){
inputOperators(target.value);
updateDisplay();
return;
}
if(target.classList.contains('decimal')){
inputDecimal(target.value);
updateDisplay();
return;
}
if(target.classList.contains('all-clear')){
resetCalculator();
updateDisplay();
return;
}
if(target.classList.contains('clear')){
}
inputDigits(target.value);
updateDisplay();
}
//Handling key presses, we've four sets of keys (numeric, operator, decimal, AC )
const keys = document.querySelector('.calculator-keys')
keys.addEventListener('click',handleInputs)
function updateDisplay(){
const display = document.querySelector('.calculator-screen')
display.value = calculator.displayValue;
}
updateDisplay();