-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
97 lines (88 loc) · 2.12 KB
/
index.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
function appendValue(value) {
document.getElementById('display').value += value;
console.log()
}
function clearDisplay() {
document.getElementById('display').value = '';
}
function calculate() {
try {
document.getElementById('display').value = eval(document.getElementById('display').value);
} catch (error) {
document.getElementById('display').value = 'Error';
}
}
document.addEventListener('keypress',function(event){
var ky = event.key;
var code = event.code;
switch(ky){
case '0':
appendValue(ky);
break;
case '1':
appendValue(ky);
break;
case '2':
appendValue(ky);
break;
case '3':
appendValue(ky);
break;
case '4':
appendValue(ky);
break;
case '5':
appendValue(ky);
break;
case '6':
appendValue(ky);
break;
case '7':
appendValue(ky);
break;
case '8':
appendValue(ky);
break;
case '9':
appendValue(ky);
break;
case '+':
appendValue(ky);
break;
case '-':
appendValue(ky);
break;
case '*':
appendValue(ky);
break;
case '/':
appendValue(ky);
break;
case '=':
calculate();
break;
case "Enter":
calculate();
break;
case 'C':
clearDisplay();
break;
default:
break;
}
})
document.addEventListener('keydown', function(event) {
if (event.keyCode === 8) {
// Handle backspace key
backspace();
}
});
function backspace() {
// Your backspace functionality here
var display = document.getElementById('display');
var currentValue = display.value;
// Remove the last character
var newValue = currentValue.slice(0, -1);
// Update the display with the new value
display.value = newValue;
}