-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
56 lines (52 loc) · 1.78 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
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
let input = document.getElementById('inputBox');
let buttons = document.querySelectorAll('button');
let string = "";
let arr = Array.from(buttons);
arr.forEach(button => {
button.addEventListener('click', (e) =>{
if(e.target.innerHTML == '='){
string = eval(string);
input.value = string;
}
else if(e.target.innerHTML == 'AC'){
string = "";
input.value = string;
}
else if(e.target.innerHTML == 'DEL'){
string = string.substring(0, string.length-1);
input.value = string;
}
else{
string += e.target.innerHTML;
input.value = string;
}
})
})
document.addEventListener('keydown', function(event) {
if (event.key >= 0 && event.key <= 9) {
string += event.key;
} else if (event.key === 'Backspace' && !event.shiftKey) {
// Only trigger delete when Shift is not pressed
string = string.substring(0, string.length - 1);
} else if (event.key === 'Enter') {
// Try to evaluate the expression and catch any errors
try {
string = eval(string).toString();
} catch (e) {
console.error(e);
string = ""; // Optionally reset the string on error
}
} else if (['/', '*', '-', '+', '%'].includes(event.key)) {
string += event.key;
} else if (event.key === 'Backspace' && event.shiftKey) {
// Clear everything if Shift + Backspace is pressed
string = "";
}
input.value = string; // Update the display after every key press
});
input.addEventListener('keyup', function(event) {
// This prevents any default action associated with the Enter key
if (event.key === 'Enter') {
event.preventDefault();
}
});