Skip to content

Commit d8504f6

Browse files
committed
counter app
1 parent a4a19d4 commit d8504f6

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

counter-app/index.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<head>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
7+
<link rel="stylesheet" href="./styles.css" />
8+
<title>Counter App</title>
9+
</head>
10+
11+
<body>
12+
13+
<div id="container">
14+
<h1 id="heading">Counter</h1>
15+
<p id="counter-id"></p>
16+
<div class="btn">
17+
<button id="decrement-btn">Decrement</button>
18+
<button id="reset-btn">Reset</button>
19+
<button id="increment-btn">Increment</button>
20+
21+
</div>
22+
<p id="instruction">Use buttons or ↑ / ↓ arrow keys. Double-click reset to confirm.</p>
23+
</div>
24+
<script src="./script.js"></script>
25+
26+
</body>
27+
28+
</html>

counter-app/script.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const counterDisplay = document.getElementById("counter-id");
2+
const incrementButton = document.getElementById("increment-btn");
3+
const decrementButton = document.getElementById("decrement-btn");
4+
const resetButton = document.getElementById("reset-btn");
5+
const container = document.getElementById("container");
6+
7+
let counter = 0;
8+
9+
function updateDisplay() {
10+
counterDisplay.textContent = counter;
11+
counterDisplay.style.fontWeight = "bold";
12+
counterDisplay.style.fontSize = "32px";
13+
incrementButton.style.padding = "5px";
14+
decrementButton.style.padding = "5px";
15+
resetButton.style.padding = "5px";
16+
container.style.textAlign = "center";
17+
incrementButton.style.backgroundColor = "#4CAF50";
18+
incrementButton.style.color = "white";
19+
decrementButton.style.color = "white";
20+
resetButton.style.color = "white";
21+
decrementButton.style.backgroundColor = "#f44336";
22+
resetButton.style.backgroundColor = "#008CBA";
23+
24+
if (counter > 0) {
25+
counterDisplay.style.color = "blue";
26+
} else if (counter < 0) {
27+
counterDisplay.style.color = "red";
28+
} else {
29+
counterDisplay.style.color = "black";
30+
}
31+
}
32+
33+
incrementButton.addEventListener("click", () => {
34+
counter++;
35+
updateDisplay();
36+
});
37+
38+
decrementButton.addEventListener("click", () => {
39+
counter--;
40+
updateDisplay();
41+
});
42+
43+
resetButton.addEventListener("click", () => {
44+
counter = 0;
45+
updateDisplay();
46+
});
47+
48+
window.addEventListener("keydown", (e) => {
49+
if (e.key === "ArrowUp") {
50+
counter++;
51+
updateDisplay();
52+
} else if (e.key === "ArrowDown") {
53+
counter--;
54+
updateDisplay();
55+
} else if (e.key === "r" || e.key === "R") {
56+
counter = 0;
57+
updateDisplay();
58+
}
59+
});
60+
61+
// Initial display update
62+
updateDisplay();

counter-app/styles.css

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
#container {
3+
4+
text-align: center;
5+
width: 300px;
6+
border: 1px solid rgb(165, 160, 160);
7+
display: inline-block;
8+
background-color: #f0f0f0;
9+
border-radius: 10px;
10+
box-shadow: #8d8686 5px 5px 15px;
11+
padding: 20px;
12+
display: flex;
13+
flex-direction: column;;
14+
align-items: center;
15+
16+
17+
18+
19+
}
20+
21+
body {
22+
font-family: Arial, sans-serif;
23+
background-color: #e0e0e0;
24+
display: flex;
25+
justify-content: center;
26+
align-items: center;
27+
height: 100vh;
28+
margin: 0;
29+
30+
}

0 commit comments

Comments
 (0)