-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
30 lines (28 loc) · 1.06 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
document.addEventListener('DOMContentLoaded', () => {
const display = document.getElementById('display');
const buttons = Array.from(document.getElementsByClassName('btn'));
buttons.map(button => {
button.addEventListener('click', (e) => {
const value = e.target.getAttribute('data-value');
switch (value) {
case 'C':
display.innerText = '0';
break;
case '=':
try {
display.innerText = eval(display.innerText.replace('÷', '/').replace('×', '*'));
} catch {
display.innerText = 'Error';
}
break;
default:
if (display.innerText === '0') {
display.innerText = value;
} else {
display.innerText += value;
}
break;
}
});
});
});