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.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
Predict and Run 10 min · Investigate 15 min · Modify 10 min · Make 15 min
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.
| What you will see | A question to ask | The fix |
|---|---|---|
| The count stays at 1. | Is the variable made inside the handler? | Move let clicks = 0 above the handler. |
| The count is right but the page does not change. | Is the new value put on the page? | Set countEl.textContent = clicks after changing clicks. |
| Reset does not fully reset. | What else did the game change besides clicks? | Put back the message and enable the button. |
| Cannot assign to a const. | Is the variable made with const and then changed? | Use let for a variable that changes. |
One possible solution for each task. Other correct programs are fine.
<p>Clicks: <span id="count">0</span></p>
<button id="click">Click me</button>
<button id="reset">Reset</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;
}
});
document.querySelector("#reset").addEventListener("click", function () {
clicks = 0;
countEl.textContent = clicks;
message.textContent = "Reach 10 clicks to win.";
button.disabled = false;
});
</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 === 5) {
message.textContent = "Halfway there!";
}
if (clicks >= 10) {
message.textContent = "You win!";
button.disabled = true;
}
});
</script>
<h1>Catch the star</h1>
<p>Score: <span id="score">0</span> Time: <span id="time">10</span></p>
<button id="star" style="position: relative; left: 40px">★</button>
<p id="message">Click the star as fast as you can.</p>
<script>
let score = 0;
let time = 10;
const scoreEl = document.querySelector("#score");
const timeEl = document.querySelector("#time");
const star = document.querySelector("#star");
const message = document.querySelector("#message");
star.addEventListener("click", function () {
if (time <= 0) {
return;
}
score = score + 1;
scoreEl.textContent = score;
star.style.left = Math.floor(Math.random() * 300) + "px";
});
const timer = setInterval(function () {
time = time - 1;
timeEl.textContent = time;
if (time <= 0) {
clearInterval(timer);
message.textContent = "Time is up! You scored " + score + ".";
}
}, 1000);
</script>
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.
Why is let clicks above the handler and not inside it?
Above, it is made once and keeps its value; inside, it would start over on every click.
Next lesson: the final challenge, a quiz page.