-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.js
More file actions
35 lines (32 loc) · 1.21 KB
/
Copy pathcalculator.js
File metadata and controls
35 lines (32 loc) · 1.21 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
document.addEventListener("DOMContentLoaded", () => {
const display = document.getElementById("display");
const buttons = document.querySelectorAll(".btn");
const themeSwitch = document.getElementById("theme-switch");
const body = document.body;
themeSwitch.addEventListener("change", () => {
body.classList.toggle("dark");
});
buttons.forEach(button => {
button.textContent = button.getAttribute("data-value");
button.addEventListener("click", () => {
const value = button.getAttribute("data-value");
if (value === "C") {
display.textContent = "0";
} else if (value === "DEL") {
display.textContent = display.textContent.slice(0, -1);
} else if (value === "=") {
try {
display.textContent = eval(display.textContent);
} catch {
display.textContent = "Error";
}
} else {
if (display.textContent === "0") {
display.textContent = value;
} else {
display.textContent += value;
}
}
});
});
});