-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
57 lines (48 loc) · 1.44 KB
/
script.js
File metadata and controls
57 lines (48 loc) · 1.44 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
const app = document.getElementById('app');
// Generate and inject the HTML content
app.innerHTML = `
<main class="container">
<h1>JavaScript Counter</h1>
<div class="counter_container">
<button id="subtract" aria-label="Decrement"><i class="fas fa-minus"></i></button>
<span id="output">0</span>
<button id="add" aria-label="Increment"><i class="fas fa-plus"></i></button>
</div>
<button id="reset" aria-label="Reset"><i class="fas fa-sync-alt"></i> Reset</button>
</main>
`;
// Cache DOM elements for performance
const addButton = document.querySelector("#add");
const subtractButton = document.querySelector("#subtract");
const resetButton = document.querySelector("#reset");
const outputSpan = document.querySelector("#output");
// Constants for counter limits
const MAX_COUNT = 1000;
const MIN_COUNT = 0;
// State
let count = 0;
// Function to update the display
const updateDisplay = () => {
outputSpan.innerText = count;
};
// Event Listeners
addButton.addEventListener("click", () => {
count++;
if (count > MAX_COUNT) {
count = MIN_COUNT;
}
updateDisplay();
});
subtractButton.addEventListener("click", () => {
count--;
if (count < MIN_COUNT) {
count = MAX_COUNT;
}
updateDisplay();
});
resetButton.addEventListener("click", () => {
count = MIN_COUNT;
updateDisplay();
});
// Initial display update
updateDisplay();