Coding Lesson
JavaScript: Interaction
Variables and a counter
Your goal: Remember a number between clicks.
Learn
A counter needs a remembered value and a visible display. The starter increases both on each click. You will add a Reset button that returns both to zero, then test the next click to catch a common mismatch between the stored number and the screen.
Walkthrough: why the code works
Distinguish the number from the paragraph
let count = 0 creates a JavaScript variable. The paragraph with id="count" is a separate HTML element. Giving them the same name does not link them automatically. The script explicitly copies the variable into the paragraph with textContent.
Follow one click
The Add a session listener runs count = count + 1, then updates the paragraph. Starting at zero, three clicks produce 1, 2 and 3. The variable is declared outside the function so it is not reset to zero at the start of each click.
Add a real Reset button
Place a second button beside the Add a session button and before the script. Give it a unique id. The HTML below belongs in body, not inside the JavaScript.
<button type="button" id="reset">Reset</button>Reset the value and the display together
Add this listener inside the existing script, after the Add listener. It assigns zero to count, then shows that value. If you only change the paragraph, the next Add click continues from the old stored number.
document.getElementById("reset").addEventListener("click", function () { count = 0; document.getElementById("count").textContent = count; });Test a sequence, not just one button
Choose Run Code. Add three sessions, reset, and add once more. Then reset twice and add again. The result should stay predictable. Run Code rebuilds the preview and reinitialises the variable; this counter is not saved progress. For an accessible enhancement, add aria-live="polite" to the count paragraph.
Expected results
Three Add activations show 3. Reset shows 0. The next Add shows 1.
Resetting when already at zero leaves the value at zero.
Running the code again starts a fresh counter at zero; it does not remember earlier sessions.
Common mistakes and how to fix them
Reset shows zero but Add jumps to four: Reset changed the displayed paragraph only. Assign count = 0 as well.
Every Add click shows one: Move let count = 0 outside the click function so it runs once when the script starts.
The counter becomes 01 or 011: Keep the stored value numeric. Use 0 rather than the string "0", and do arithmetic on count rather than the paragraph’s text.
View starter code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My hobby page</title>
<style></style>
</head>
<body>
<h1>Practice counter</h1>
<p id="count">0</p>
<button id="add">Add a session</button>
<script>
let count = 0;
document.getElementById("add").addEventListener("click", function () {
count = count + 1;
document.getElementById("count").textContent = count;
});
</script>
</body>
</html>Your challenge
Add a second button that resets the counter to zero.
Keep developing your hobby page: Download HTML before leaving the sandbox, then choose Open HTML file when returning to your project. Load saved draft restores only this lesson’s draft in this browser.
Practise in the sandboxShow a hint
Give the new button id="reset" and set both count and the displayed text to 0 in its click handler.
Show a sample answer
Try the challenge first. This is one possible answer or a snippet to adapt.
<button id="reset">Reset</button>
// Add inside the script:
document.getElementById("reset").addEventListener("click", function () {
count = 0;
document.getElementById("count").textContent = count;
});Check your work
Three clicks show 3; Reset shows 0; another click shows 1.
Completion is your own check, not an automatic grade.
