-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
139 lines (124 loc) · 3.63 KB
/
script.js
File metadata and controls
139 lines (124 loc) · 3.63 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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const display = document.getElementById("display");
const history = document.getElementById("history");
const historyList = document.getElementById("history-list");
const clearHistoryBtn = document.getElementById("clear-history");
const buttons = document.querySelectorAll(".numpad button");
const themeSwitcher = document.getElementById("themeSwitcher");
let expression = "";
let openBrackets = 0;
let calcHistory = [];
if (localStorage.getItem("theme") === "dark") {
document.body.classList.add("dark");
themeSwitcher.textContent = "☀️ Light Mode";
}
themeSwitcher.addEventListener("click", () => {
document.body.classList.toggle("dark");
const isDark = document.body.classList.contains("dark");
themeSwitcher.textContent = isDark ? "☀️ Light Mode" : "🌙 Dark Mode";
localStorage.setItem("theme", isDark ? "dark" : "light");
});
function isOperator(char) {
return ["+", "-", "*", "/", "%"].includes(char);
}
function parseExpression(expr) {
try {
expr = expr.replace(/--/g, "+");
return Function('"use strict";return (' + expr + ")")();
} catch {
return "Error";
}
}
function updateDisplay() {
display.value = expression;
}
function updateLocalHistoryDisplay() {
historyList.innerHTML = calcHistory
.slice()
.reverse()
.map((item) => `<div>${item}</div>`)
.join("");
}
function saveToHistory(line) {
calcHistory.push(line);
if (calcHistory.length > 5) {
calcHistory.shift();
}
updateLocalHistoryDisplay();
}
function handleInput(val) {
if (val === "C") {
expression = "";
openBrackets = 0;
history.textContent = ""; // Clear previous calculation above display
} else if (val === "=") {
if (expression === "") return;
const result = parseExpression(expression);
if (result === "Error") {
history.textContent = "Invalid Expression";
expression = "";
} else {
history.textContent = expression + " =";
saveToHistory(`${expression} = ${result}`);
expression = result.toString();
}
} else if (val === "DEL") {
if (expression.slice(-1) === "(") openBrackets--;
if (expression.slice(-1) === ")") openBrackets++;
expression = expression.slice(0, -1);
} else if (val === "()") {
const last = expression.slice(-1);
if (
openBrackets === 0 ||
isOperator(last) ||
last === "(" ||
expression === ""
) {
expression += "(";
openBrackets++;
} else if (openBrackets > 0) {
expression += ")";
openBrackets--;
}
} else {
const lastChar = expression.slice(-1);
if (isOperator(lastChar) && isOperator(val)) return;
expression += val;
}
updateDisplay();
}
buttons.forEach((btn) => {
btn.addEventListener("click", () =>
handleInput(btn.getAttribute("data-value"))
);
});
document.addEventListener("keydown", (e) => {
const key = e.key;
if (/\d/.test(key)) {
expression += key;
} else if (["+", "-", "*", "/", "%"].includes(key)) {
const last = expression.slice(-1);
if (!isOperator(last)) expression += key;
} else if (key === "Enter") {
handleInput("=");
return;
} else if (key === "Backspace") {
handleInput("DEL");
return;
} else if (key === "Escape") {
handleInput("C");
return;
} else if (key === "(" || key === ")") {
handleInput("()");
return;
} else if (key === ".") {
expression += ".";
} else {
return;
}
updateDisplay();
});
// Clear History Button
clearHistoryBtn.addEventListener("click", () => {
calcHistory = [];
updateLocalHistoryDisplay();
});