-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
36 lines (33 loc) · 931 Bytes
/
script.js
File metadata and controls
36 lines (33 loc) · 931 Bytes
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
// Button Labels
const buttonLabels = [
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"C", "0", "=", "+"
];
// Variables
let inputField = document.getElementById("calcInput");
let currentInput = "";
// Dynamically Create Buttons
const buttonsContainer = document.querySelector(".buttons");
buttonLabels.forEach((label) => {
const button = document.createElement("button");
button.textContent = label;
button.addEventListener("click", () => handleButtonClick(label));
buttonsContainer.appendChild(button);
});
// Handle Button Clicks
function handleButtonClick(label) {
if (label === "=") {
try {
currentInput = eval(currentInput).toString();
} catch (e) {
currentInput = "Error";
}
} else if (label === "C") {
currentInput = "";
} else {
currentInput += label;
}
inputField.value = currentInput;
}