Students build and extend a quiz page from an array of questions, buttons made for each choice, click handlers, a score and a question number, fix an answer check that is always true, add colour feedback and a total, and make a quiz 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 15 min · Make 25 min
Read the unit primer first. This is the final challenge; it marks "Challenge complete". It mirrors the p5.js and Python challenges: a list of data, a loop, a handler, a score. Everything has been taught; the new part is putting it together. The questions are an array of objects, buttons are made from the choices, check(choice) is the handler, current and score are the only variables that change. The bug is a single = in an if: no error, every answer is right; the p5 course had > where < belonged. Quizzes that save results or send them anywhere are out of scope: the playground sends nothing. Large projects belong to the teacher (a unit project, a class quiz bank).
Where the data lives: the page is built from the array, and only current and score change. Draw those two boxes on the board.
| What you will see | A question to ask | The fix |
|---|---|---|
| Every answer is correct. | Does the check use ===? | Compare with three equal signs. |
| Cannot read properties of undefined (reading text). | Has current gone past the end of the array? | Stop at questions.length before showing the next question. |
| The buttons pile up. | Is the old choices box emptied first? | Set choicesEl.textContent = "" before making new buttons. |
| The score is wrong after Play again. | Was the score set back to 0? | Reset current, score and the text on the page. |
One possible solution for each task. Other correct programs are fine.
<h1>Capital cities quiz</h1>
<p id="question"></p>
<div id="choices"></div>
<p id="feedback"></p>
<p>Score: <span id="score">0</span></p>
<script>
const questions = [
{ text: "What is the capital of France?", choices: ["Paris", "Rome", "Madrid"], answer: "Paris" },
{ text: "What is the capital of Japan?", choices: ["Seoul", "Tokyo", "Beijing"], answer: "Tokyo" },
{ text: "What is the capital of Australia?", choices: ["Sydney", "Canberra", "Perth"], answer: "Canberra" },
];
let current = 0;
let score = 0;
const questionEl = document.querySelector("#question");
const choicesEl = document.querySelector("#choices");
const feedbackEl = document.querySelector("#feedback");
const scoreEl = document.querySelector("#score");
function showQuestion() {
const q = questions[current];
questionEl.textContent = q.text;
choicesEl.textContent = "";
for (const choice of q.choices) {
const button = document.createElement("button");
button.textContent = choice;
button.addEventListener("click", function () {
check(choice);
});
choicesEl.append(button);
}
}
function check(choice) {
if (choice === questions[current].answer) {
score = score + 1;
scoreEl.textContent = score;
feedbackEl.textContent = "Correct!";
feedbackEl.style.color = "green";
} else {
feedbackEl.textContent = "Not quite: it was " + questions[current].answer;
feedbackEl.style.color = "crimson";
}
current = current + 1;
if (current < questions.length) {
showQuestion();
} else {
questionEl.textContent = "Finished! Score: " + score;
choicesEl.textContent = "";
}
}
showQuestion();
</script>
<h1>Capital cities quiz</h1>
<p id="question"></p>
<div id="choices"></div>
<p id="feedback"></p>
<p>Score: <span id="score">0</span></p>
<script>
const questions = [
{ text: "What is the capital of France?", choices: ["Paris", "Rome", "Madrid"], answer: "Paris" },
{ text: "What is the capital of Japan?", choices: ["Seoul", "Tokyo", "Beijing"], answer: "Tokyo" },
{ text: "What is the capital of Australia?", choices: ["Sydney", "Canberra", "Perth"], answer: "Canberra" },
];
let current = 0;
let score = 0;
const questionEl = document.querySelector("#question");
const choicesEl = document.querySelector("#choices");
const feedbackEl = document.querySelector("#feedback");
const scoreEl = document.querySelector("#score");
function showQuestion() {
const q = questions[current];
questionEl.textContent = q.text;
choicesEl.textContent = "";
for (const choice of q.choices) {
const button = document.createElement("button");
button.textContent = choice;
button.addEventListener("click", function () {
check(choice);
});
choicesEl.append(button);
}
}
function check(choice) {
if (choice === questions[current].answer) {
score = score + 1;
scoreEl.textContent = score;
feedbackEl.textContent = "Correct!";
} else {
feedbackEl.textContent = "Not quite: it was " + questions[current].answer;
}
current = current + 1;
if (current < questions.length) {
showQuestion();
} else {
questionEl.textContent = "Finished! You scored " + score + " of " + questions.length;
choicesEl.textContent = "";
}
}
showQuestion();
</script>
<h1>Space quiz</h1>
<p id="question"></p>
<div id="choices"></div>
<p id="feedback"></p>
<p>Score: <span id="score">0</span></p>
<button id="again" class="hidden">Play again</button>
<style>.hidden { display: none; }</style>
<script>
const questions = [
{ text: "Which planet is closest to the Sun?", choices: ["Mercury", "Venus", "Mars"], answer: "Mercury" },
{ text: "What is the Moon?", choices: ["A star", "A moon of Earth", "A planet"], answer: "A moon of Earth" },
{ text: "Which planet has rings you can see easily?", choices: ["Saturn", "Mars", "Earth"], answer: "Saturn" },
{ text: "What do we call our galaxy?", choices: ["Andromeda", "The Milky Way", "Orion"], answer: "The Milky Way" },
{ text: "How many planets orbit the Sun?", choices: ["Eight", "Nine", "Seven"], answer: "Eight" },
];
let current = 0;
let score = 0;
const questionEl = document.querySelector("#question");
const choicesEl = document.querySelector("#choices");
const feedbackEl = document.querySelector("#feedback");
const scoreEl = document.querySelector("#score");
const again = document.querySelector("#again");
function showQuestion() {
const q = questions[current];
questionEl.textContent = q.text;
choicesEl.textContent = "";
for (const choice of q.choices) {
const button = document.createElement("button");
button.textContent = choice;
button.addEventListener("click", function () {
check(choice);
});
choicesEl.append(button);
}
}
function check(choice) {
if (choice === questions[current].answer) {
score = score + 1;
scoreEl.textContent = score;
feedbackEl.textContent = "Correct!";
} else {
feedbackEl.textContent = "Not quite: it was " + questions[current].answer;
}
current = current + 1;
if (current < questions.length) {
showQuestion();
} else {
questionEl.textContent = "Finished! You scored " + score + " of " + questions.length;
choicesEl.textContent = "";
again.classList.remove("hidden");
}
}
again.addEventListener("click", function () {
current = 0;
score = 0;
scoreEl.textContent = score;
feedbackEl.textContent = "";
again.classList.add("hidden");
showQuestion();
});
showQuestion();
</script>
Stretch: Shuffle the questions, add a timer with setInterval, or give harder questions more points using a property on each object.
Support: Give the whole quiz and ask students to add only questions of their own, then the colour feedback.
Which two variables change as the quiz is played?
current, the number of the question on the screen, and score, how many were right.
Optional next lesson: the real browser console. Beyond the course: the Python web apps course, to keep results between visits.