-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
28 lines (27 loc) · 876 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
const display = document.querySelector(".result");
const buttons = document.querySelectorAll(".calculator-number-buttons");
let currentOperation = "";
buttons.forEach((button) => {
button.addEventListener("click", () => {
const value = button.textContent;
if (value === "clear") {
currentOperation = "";
display.textContent = "0";
} else if (value === "DEL") {
currentOperation = currentOperation.slice(0, -1);
display.textContent = currentOperation || "0";
} else {
currentOperation += value;
display.textContent = currentOperation;
}
});
});
const equalsButton = document.querySelector(".equals-buttom");
equalsButton.addEventListener("click", () => {
try {
currentOperation = eval(currentOperation);
display.textContent = currentOperation;
} catch (error) {
display.textContent = "Error";
}
});