forked from thinkswell/javascript-mini-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
49 lines (47 loc) · 1.67 KB
/
index.js
File metadata and controls
49 lines (47 loc) · 1.67 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
43
44
45
46
47
48
49
const display = document.querySelector("#display");
const buttons = document.querySelectorAll("button");
buttons.forEach((item) => {
item.onclick = () => {
if (item.id == "clear") {
display.innerText = "";
} else if (item.id == "backspace") {
let string = display.innerText.toString();
display.innerText = string.substr(0, string.length - 1);
} else if (display.innerText != "" && item.id == "equal") {
try {
if (display.innerText.includes("/0")) {
throw new Error("Division by zero");
}
display.innerText = Function(
'"use strict";return (' + display.innerText + ")"
)();
} catch (e) {
display.innerText = "Cannot divide by zero!";
setTimeout(() => (display.innerText = ""), 2000);
}
} else if (display.innerText == "" && item.id == "equal") {
display.innerText = "Empty!";
setTimeout(() => (display.innerText = ""), 2000);
} else {
const lastChar = display.innerText.slice(-1);
if (
["+", "-", "*", "/"].includes(lastChar) &&
["+", "-", "*", "/"].includes(item.innerText)
) {
// Prevent adding multiple consecutive operators
display.innerText = display.innerText.slice(0, -1) + item.innerText;
} else {
display.innerText += item.innerText;
}
}
};
});
const themeToggleBtn = document.querySelector(".theme-toggler");
const calculator = document.querySelector(".calculator");
const toggleIcon = document.querySelector(".toggler-icon");
let isDark = true;
themeToggleBtn.onclick = () => {
calculator.classList.toggle("dark");
themeToggleBtn.classList.toggle("active");
isDark = !isDark;
};