-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
42 lines (36 loc) · 1.04 KB
/
script.js
File metadata and controls
42 lines (36 loc) · 1.04 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
40
41
42
let string = "";
const display = document.querySelector('input');
const buttons = document.querySelectorAll('.button');
buttons.forEach((button) => {
button.addEventListener('click', (e) => {
const target = e.target;
// equal button
if (target.closest('.equal')) {
try {
string = eval(string);
display.value = string;
} catch {
display.value = "Error";
string = "";
}
}
// AC button
else if(target.innerHTML === 'AC') {
string = "";
display.value = string;
}
else if(target.closest('.button-delete')) {
string = string.slice(0, -1);
display.value = string;
}
else if(target.dataset.value) {
string += target.dataset.value;
display.value = string;
}
// numbers / Dot
else {
string += target.innerHTML;
display.value = string;
}
});
});