-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpractice.js
33 lines (31 loc) · 947 Bytes
/
practice.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
const screen = document.getElementById('screen');
const buttons = document.querySelectorAll('button');
let currentInput = '';
let previousInput = '';
let operator = '';
buttons.forEach(button => {
button.addEventListener('click', () => {
const value = button.value;
if (value === 'all-clear') {
currentInput = '';
previousInput = '';
operator = '';
screen.value = '';
} else if (value === '=') {
if (previousInput !== '' && operator !== '') {
currentInput = eval(`${previousInput} ${operator} ${currentInput}`);
screen.value = currentInput;
operator = '';
}
} else if (['+', '-', '*', '/'].includes(value)) {
if (currentInput !== '') {
operator = value;
previousInput = currentInput;
currentInput = '';
}
} else {
currentInput += value;
screen.value = currentInput;
}
});
});