-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
116 lines (103 loc) · 3.18 KB
/
script.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
// Object to hold data needed to calculate output - uses immediate execution logic
const calculator = {
displayValue: '0',
varA: null,
operator: null,
varB: null,
readyForVarB: false
};
// Function to update display value of calculator
function updateDisplay() {
const display = document.querySelector('.current-display');
display.value = calculator.displayValue;
}
updateDisplay();
// Clear all calculator settings
function resetCalculator() {
calculator.displayValue = '0';
calculator.varA = null;
calculator.operator = null;
calculator.readyForVarB = false;
}
/* Main key press handler for listening to button presses
Button click states are either:
1. Digits (0-9)
2. Operators (+ - * / =)
3. Decimal point (.)
4. Reset (AC)
*/
const keys = document.querySelector('.calculator');
keys.addEventListener('click', (event) => {
const target = event.target;
// If a click was NOT on a calculator button, then exit function.
if (!target.matches('button')) {
return;
}
if (target.classList.contains('operator')) {
handleOperator(target.value);
updateDisplay();
return;
}
if (target.classList.contains('decimal')) {
inputDecimal(target.value);
updateDisplay();
return;
}
if (target.classList.contains('all-clear')) {
resetCalculator();
updateDisplay();
return;
}
inputDigit(target.value);
updateDisplay();
});
/* Overwrite initial display '0' with number input if a number is pressed.
Append digits if additional numbers are pressed */
function inputDigit(digit) {
const displayValue = calculator.displayValue;
const readyForVarB = calculator.readyForVarB;
if (readyForVarB === true) {
calculator.displayValue = digit;
calculator.readyForVarB = false;
} else {
calculator.displayValue = displayValue === '0' ? digit : displayValue + digit;
}
}
/* Add a decimal point to the display value if there isn't already a decimal point present.
Only ONE decimal point allowed */
function inputDecimal(decimal) {
if (calculator.waitingForSecondOperand === true) {
return;
}
if (!calculator.displayValue.includes(decimal)) {
calculator.displayValue += decimal;
}
}
function handleOperator(nextOperator) {
const varA = calculator.varA;
const displayValue = calculator.displayValue;
const operator = calculator.operator;
const inputValue = parseFloat(displayValue); // Convert input into a number with decimals
if (operator && calculator.readyForVarB) {
calculator.operator = nextOperator;
return;
}
// Store converted number into varA if no value exists yet
if (varA === null) {
calculator.varA = inputValue;
} else if (operator) {
const currentValue = varA || 0;
const result = performCalculation[operator](currentValue, inputValue);
calculator.displayValue = String(result);
calculator.varA = result;
}
calculator.readyForVarB = true;
calculator.operator = nextOperator;
}
const performCalculation = {
'+': (varA, readyForVarB) => varA + readyForVarB,
'-': (varA, readyForVarB) => varA - readyForVarB,
'*': (varA, readyForVarB) => varA * readyForVarB,
'/': (varA, readyForVarB) => varA / readyForVarB,
'=': (varA, readyForVarB) => readyForVarB,
};