-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
43 lines (38 loc) · 871 Bytes
/
script.js
File metadata and controls
43 lines (38 loc) · 871 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
37
38
39
40
41
42
43
const display = document.getElementById('display');
function appendToDisplay(input) {
display.value += input;
}
function clearDisplay() {
display.value = "";
}
function calculate() {
try {
display.value = eval(display.value);
} catch (error) {
display.value = "Error";
}
}
// Enable keyboard support for the calculator
document.addEventListener('keydown', function (event) {
const key = event.key;
// Append numbers, dot, and operators to the display
if (
(key >= '0' && key <= '9') ||
key === '.' ||
key === '+' ||
key === '-' ||
key === '*' ||
key === '/'
) {
appendToDisplay(key);
}
// Call calculate() when Enter is pressed
else if (key === 'Enter') {
event.preventDefault();
calculate();
}
// Clear display when Escape is pressed
else if (key === 'Escape') {
clearDisplay();
}
});