-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
41 lines (31 loc) · 1012 Bytes
/
main.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
const screen = document.querySelector("[data-screen]");
const btns = document.querySelectorAll("[data-btn]");
const equal = document.querySelector("[data-equal]");
const clear = document.querySelector("[data-clear]");
const del = document.querySelector("[data-del]");
// showing clicked button values on the screen
btns.forEach((btn) => {
btn.addEventListener("click", (e) => {
screen.innerText === '0' ? screen.innerText = e.target.innerText : screen.innerText += e.target.innerText;
});
});
// evaluation
equal.addEventListener("click", () => {
try {
screen.innerText = eval(screen.innerText);
} catch (error) {
alert("Invalid Input");
screen.innerText = '0';
}
});
// clear the screen
clear.addEventListener("click", () => {
screen.innerText = '0';
});
// deleting one value
del.addEventListener("click", () => {
screen.innerText = screen.innerText.slice(0, -1);
if (screen.innerText === '') {
screen.innerText = '0';
}
})