-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
74 lines (70 loc) · 2.4 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
/* Theme Toggle */
const toggles = document.querySelectorAll(".toggle");
const btns = document.querySelectorAll(".num");
const val = document.querySelector(".value");
const one = document.querySelector(".one");
const two = document.querySelector(".two");
const three = document.querySelector(".three");
val.textContent = "";
toggles.forEach((toggle) => {
toggle.addEventListener("click", () => {
document.body.classList = "";
toggles.forEach((tog) => tog.classList.remove("toggle-background"));
toggle.classList.add("toggle-background");
if (toggle.classList.contains("one")) {
document.body.classList = "theme-1";
localStorage.setItem("theme", "theme-1");
} else if (toggle.classList.contains("two")) {
document.body.classList = "theme-2";
localStorage.setItem("theme", "theme-2");
} else if (toggle.classList.contains("three")) {
document.body.classList = "theme-3";
localStorage.setItem("theme", "theme-3");
}
toggle.classList.add("toggle-background");
});
});
function checkTheme() {
const localStorageTheme = localStorage.getItem("theme");
if (localStorageTheme != null) {
document.body.classList = localStorageTheme;
toggles.forEach((tog) => tog.classList.remove("toggle-background"));
if (localStorageTheme == "theme-1") one.classList.add("toggle-background");
else if (localStorageTheme == "theme-2")
two.classList.add("toggle-background");
else if (localStorageTheme == "theme-3")
three.classList.add("toggle-background");
}
}
window.onload = checkTheme();
btns.forEach((btn) => {
btn.addEventListener("click", () => {
if (val.textContent == "ERROR") val.textContent = "";
switch (btn.textContent) {
case "DEL":
val.textContent = val.textContent.slice(0, -1);
break;
case "x":
val.textContent += "*";
break;
case "RESET":
val.textContent = "";
break;
case "=":
try {
val.textContent = eval(val.textContent);
if (
val.textContent == "NaN" ||
val.textContent == "undefined" ||
val.textContent == "Infinity"
)
val.textContent = "ERROR";
} catch {
val.textContent = "ERROR";
}
break;
default:
val.textContent += btn.textContent;
}
});
});