document.querySelector('button').addEventListener('click', () => {
let n = document.querySelector('.cscore').innerHTML
if (n === 3) {
n = 0
}
n++
document.querySelector('.cscore').innerHTML = n
})
Thank you. Is it weird that I have to reset it to -1 for it to be reset to 0 after reaching 3? Also, should I ideally reset it to a number or to a string?
I’m going off on a bit of a tangent here, but I would suggest you be wary of holding your app state inside the DOM (right now the n value depends entirely on some element’s innerHtml). If instead you maintain your state in JavaScript, and update the DOM based on the state, then you won’t need to worry so much about converting between data types and so on.
For example:
let score = 0;
document.querySelector("button").addEventListener("click", () => {
incrementScore();
renderScore();
});
function incrementScore() {
if (score === 3) {
score = 0;
return;
}
score++;
}
function renderScore() {
document.querySelector(".cscore").textContent = score;
}
This is just one way to do it, the sky’s the limit of course.