-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
36 lines (31 loc) · 876 Bytes
/
script.js
File metadata and controls
36 lines (31 loc) · 876 Bytes
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
let display = document.getElementById('display');
function appendToDisplay(value) {
if (display.value === '0' && value !== '.') {
display.value = value;
} else {
display.value += value;
}
}
function clearDisplay() {
display.value = '0';
}
function calculate() {
try {
display.value = eval(display.value) || '0';
} catch (error) {
display.value = 'Error';
setTimeout(clearDisplay, 1000);
}
}
// Keyboard input handling
document.addEventListener('keydown', (event) => {
const key = event.key;
const validKeys = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '+', '-', '*', '/', '(', ')'];
if (validKeys.includes(key)) {
appendToDisplay(key);
} else if (key === 'Enter') {
calculate();
} else if (key === 'Escape' || key === 'c' || key === 'C') {
clearDisplay();
}
});