|
| 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(); |
0 commit comments