Keep a number between clicks in a variable made above the handler, and use an if to decide when something happens.
A score, a basket count and a "5 likes" all work the same way: a variable that lives between clicks.
A page can remember things in variables, as your p5 sketches did. The rule is where you make them:
let clicks = 0; // made once, above the handler
button.addEventListener("click", function () {
clicks = clicks + 1; // changed on every click
countEl.textContent = clicks; // shown on the page
});
A variable made above the handler keeps its value between clicks. One made inside is made again on each click, so it starts over.
An if inside the handler decides when something happens, such as winning at 10.
The hard part is seeing that the number on the page is not the memory. The variable remembers; the page only shows it. Change the variable, then show it.
Make your prediction first. Help opens once you've locked it in.
<p>Clicks: <span id="count">0</span></p>
<button id="click">Click me</button>
<script>
let clicks = 0;
const countEl = document.querySelector("#count");
document.querySelector("#click").addEventListener("click", function () {
clicks = clicks + 1;
countEl.textContent = clicks;
});
</script>
Is clicks made once, or on every click?
Press Run and click the button ten times. The if inside the handler decides when you win, and button.disabled = true stops the button.
<p>Clicks: <span id="count">0</span></p>
<button id="click">Click me</button>
<p id="message">Reach 10 clicks to win.</p>
<script>
let clicks = 0;
const countEl = document.querySelector("#count");
const message = document.querySelector("#message");
const button = document.querySelector("#click");
button.addEventListener("click", function () {
clicks = clicks + 1;
countEl.textContent = clicks;
if (clicks >= 10) {
message.textContent = "You win!";
button.disabled = true;
}
});
</script>
<p>Clicks: <span id="count">0</span></p>
<button id="click">Click me</button>
<p id="message">Reach 10 clicks to win.</p>
<script>
let clicks = 0;
const countEl = document.querySelector("#count");
const message = document.querySelector("#message");
const button = document.querySelector("#click");
button.addEventListener("click", function () {
clicks = clicks + 1;
countEl.textContent = clicks;
if (clicks >= 10) {
message.textContent = "You win!";
button.disabled = true;
}
});
</script>
Memory, change and show.
The page is only a picture.
The count never gets past 1.
Match each line with what it does.
The page shows 1 on every click and never goes higher. There is no red error. Click the line that is wrong.
Put everything back.
A reset button needs to put back everything the game changed.
Set clicks back to 0, show it on the page, and also put the message and the button back.

A second if.
The game already has one if. A second if can look for a different number.
if (clicks === 5) { message.textContent = "Halfway there!"; } inside the handler.

Add a Reset button that starts the game again.
This ticks when your page has all of the above.
Show Halfway there! when the count reaches 5.
This ticks when your page has all of the above.
Make a small game: click a moving star, press as fast as you can, guess a number. It needs a score variable made above the handler, a goal or a time limit, and a message when you win or lose. The page on this lesson moves a star and stops after 10 seconds, using setInterval.
You can now keep a score. Next: the final challenge, a quiz page.
Students keep a count in a variable made above a click handler, show it on the page, win at ten with an if, find a variable made inside the handler that starts over, and make a clicker game of their own. About 50 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
Read the unit primer first. This is the Python "global" and state idea in JavaScript: a variable made above a function is shared and kept; one made inside is made again each call. The p5 sketches kept values in let above draw; here the handler plays the part of draw. The page is only a picture of the variable: change the variable, then put it on the page. The bug has no red error. button.disabled = true is a property like textContent. The Make example uses setInterval (run a function every 1000 ms) and clearInterval; students can use it as a recipe, and a number of them will want it.
The page shows the variable; it is not the variable. Ask "what does the page know about clicks?" and the answer is nothing.
Stretch: Make the goal change each game (a random target), or keep a best score that survives a reset.
Support: Give the counter and ask students for only the win message.
Next lesson: the final challenge, a quiz page.