-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
39 lines (30 loc) · 1.33 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
const calculator = new Calculator();
window.addEventListener('load', () => {
const operators = document.querySelectorAll('.operations');
document
.querySelectorAll('.number')
.forEach(num => num.addEventListener('click', e => calculator.addNumber(e.target.textContent)));
document.getElementById('decimal').addEventListener('click', () => calculator.addDecimal());
document.getElementById('clear').addEventListener('click', () => {
calculator.clear();
operators.forEach(operator => operator.classList.remove('selected'));
});
operators.forEach(operator => {
operator.addEventListener('click', e => {
// handle consecutive operations
if (calculator.operation && calculator.secondFactor) calculator.handleOperation();
// change active operator
if (calculator.operation !== e.target.textContent) {
operators.forEach(operator => operator.classList.remove('selected'));
calculator.operation = e.target.textContent;
e.target.classList.add('selected');
}
});
});
document.getElementById('equal').addEventListener('click', () => {
calculator.handleOperation();
calculator.operation = null;
operators.forEach(operator => operator.classList.remove('selected'));
});
document.getElementById('delete').addEventListener('click', () => calculator.delete());
});