-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
39 lines (36 loc) · 1.11 KB
/
script.js
File metadata and controls
39 lines (36 loc) · 1.11 KB
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 display = document.getElementById('display');
const buttons = document.querySelectorAll('.button');
buttons.forEach(button => {
button.addEventListener('click', () => {
const value = button.getAttribute('data-value');
if (button.id === 'clear') {
display.value = '';
} else if (button.id === 'delete') {
display.value = display.value.slice(0, -1);
} else if (button.id === 'equals') {
try {
display.value = eval(display.value) || '';
} catch {
display.value = 'Error';
}
} else if (value !== null) {
display.value += value;
}
});
});
// Keyboard support
document.addEventListener('keydown', e => {
if ((e.key >= '0' && e.key <= '9') || ['+', '-', '*', '/', '.'].includes(e.key)) {
display.value += e.key;
} else if (e.key === 'Enter') {
try {
display.value = eval(display.value) || '';
} catch {
display.value = 'Error';
}
} else if (e.key === 'Backspace') {
display.value = display.value.slice(0, -1);
} else if (e.key === 'Escape') {
display.value = '';
}
});