-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
33 lines (29 loc) · 1.04 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
// script.js
const display = document.getElementById('display');
const buttons = document.querySelectorAll('button');
let currentOperation = '';
let previousOperation = '';
buttons.forEach((button) => {
button.addEventListener('click', (e) => {
const buttonText = e.target.textContent;
if (buttonText === 'C') {
currentOperation = '';
display.value = '';
} else if (buttonText === '=') {
try {
const result = eval(currentOperation);
display.value = result;
previousOperation = currentOperation;
currentOperation = '';
} catch (error) {
display.value = 'Error';
}
} else if (buttonText === '+' || buttonText === '-' || buttonText === '*' || buttonText === '/') {
currentOperation += buttonText;
display.value = currentOperation;
} else {
currentOperation += buttonText;
display.value = currentOperation;
}
});
});