Put it all together: a list of questions as data, buttons made for each choice, a score that remembers, and feedback on every click.
A quiz is a list of questions, a number for where you are, a score, and a click: everything you have learned in one page.
Your quiz keeps its questions as data, in an array of objects, and uses everything from the course:
questions[current].text),createElement makes a button for each choice,current (which question) and score,if decides what to say, and another decides when the quiz is over.The hard part is keeping track of what changes. Only two variables change: current and score. Everything on the page is a picture of them and of questions[current].
Make your prediction first. Help opens once you've locked it in.
<p id="q">What is 3 + 4?</p>
<button id="a1">6</button>
<button id="a2">7</button>
<p id="result"></p>
<script>
function pick(answer) {
const result = document.querySelector("#result");
if (answer === 7) {
result.textContent = "Correct!";
} else {
result.textContent = "Try again";
}
}
document.querySelector("#a1").addEventListener("click", function () {
pick(6);
});
document.querySelector("#a2").addEventListener("click", function () {
pick(7);
});
</script>
Does the second click add to the first message, or replace it?
Play the quiz: three questions, a score and a message at the end. The questions are an array of objects at the top; the rest of the page is built from them.
<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! 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! Score: " + score;
choicesEl.textContent = "";
}
}
showQuestion();
</script>
Data, buttons, memory and decisions.
Where the data lives.
Every answer is right.
Match each piece with what it does.
Every answer says Correct!, even the wrong ones. There is no red error. Click the line that is wrong.
Right is green, wrong is red.
The feedback line is a paragraph like any other. What can you change about it?
Inside each branch, set feedbackEl.style.color: green when correct, crimson when not.

Use the length of the array.
The end message is built by joining pieces of text.
Join "You scored ", the score, " of " and questions.length.

Show the feedback in green when right and crimson when wrong.
This ticks when your page has all of the above.
Finish with You scored 2 of 3 instead of Score: 2.
This ticks when your page has all of the above.
Make your own quiz on a topic you know well, with at least five questions, a score, feedback on every answer, and a Play again button. Then ask a friend to play it, watch what they do, and change one thing. The page on this lesson is a space quiz. Large projects, such as many quizzes or a timer, belong in your own time.
Challenge complete when the three required checks are done. From here: the real browser console (optional), a bigger project of your own, or the Python web apps course for pages that remember.
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. About 65 minutes.
Open the teacher guide: answers, solutions, slides and a worksheet.
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.
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.
Optional next lesson: the real browser console. Beyond the course: the Python web apps course, to keep results between visits.