I've encountered an issue with my simple counter code - it's not functioning properly. The goal is for the decrement function to stop running when the count reaches 0. I'd appreciate any help in troubleshooting what might be wrong.
let count = 0;
let displayCount = document.getElementById("count")
let incrementElement = document.getElementById("increment");
let decrementElement = document.getElementById("decrement");
let resetElement = document.getElementById("reset");
function increment() {
let count = 0
count++;
}
function decrement() {
displayCount.textContent = count;
if (count !== 0) {
count--;
}
}
function reset() {
displayCount.textContent = count;
count = 0;
}
displayCount = incrementElement.addEventListener("click", increment);
decrementElement.addEventListener("click", decrement);
resetElement.addEventListener("click", reset);
The code provided above was written by me, but unfortunately, when clicking the buttons, it seems to progress forward before functioning as intended. For instance, if the count is at 4 and the decrement button is clicked, it increments to 5 before going back down to 4, 3, 2, 1, 0.