How it will work
You’ll build a quiz: a question, three choices, click to answer, and the score shows up at the end. The quiz data (questions and answers) will live in an array of objects: that sounds fancy, but you’ll see it’s just a neatly organized list.
Show me: the quiz data
const questions = [
{
text: "How many legs does a spider have?",
choices: ["6", "8", "10"],
correct: 1,
},
{
text: "Which animal is the fastest?",
choices: ["cheetah", "falcon", "hare"],
correct: 1,
},
{
text: "What's the name of our website?",
choices: ["Code with Martin", "Code with Charlie", "Web with Wendy"],
correct: 0,
},
];
- An array
[...]is a list of values: you know this from before (photoswas also an array). - An object
{...}is a box with named compartments:text,choices,correct. correct: 1means: choice number 1 is correct: counting starts at zero, so it’s the second one in order.
Show me: the quiz engine
HTML:
<div class="card">
<h2 id="quiz-question"></h2>
<div id="quiz-choices"></div>
<p id="quiz-status"></p>
</div>
JavaScript:
const questionEl = document.getElementById("quiz-question");
const choicesEl = document.getElementById("quiz-choices");
const statusEl = document.getElementById("quiz-status");
let questionNumber = 0;
let points = 0;
function showQuestion() {
const question = questions[questionNumber];
questionEl.textContent = question.text;
choicesEl.innerHTML = ""; // clear the old buttons
question.choices.forEach((choice, index) => {
const button = document.createElement("button");
button.textContent = choice;
button.addEventListener("click", () => answer(index));
choicesEl.appendChild(button);
});
statusEl.textContent = "Question " + (questionNumber + 1) + " of " + questions.length;
}
function answer(index) {
if (index === questions[questionNumber].correct) {
points++;
}
questionNumber++;
if (questionNumber < questions.length) {
showQuestion();
} else {
questionEl.textContent = "Done! 🎉";
choicesEl.innerHTML = "";
statusEl.textContent = "You scored " + points + " out of " + questions.length + " points.";
}
}
showQuestion();
New stuff:
document.createElement("button"): JavaScript can not only find elements, but also create them. We create a button for every choice.appendChild(...): insert the created element into the page.innerHTML = "": empty out an element’s contents (we clear the old buttons before the next question).- Two functions pass work to each other:
showQuestion()draws,answer()evaluates. Splitting a program into small functions is a pro habit.
Your turn 💪
Build a quiz with your own questions, at least five. Project ideas:
- 🏫 Quiz about our school: how old is the school? Who’s the principal? How many classrooms do we have?
- 🙋 How well do you know me?: my favorite food? How old am I? What’s my dog’s name?
- 🌍 Travel quiz: capital of Croatia? Highest mountain in your country? Which direction is the sea?
- ⚽ How well do you know our team?: who’s the captain? How many goals did Tom score? What are the club colors?
Just swap the contents of the questions array: the quiz engine stays the same. That’s exactly why data and logic are kept separate!
Bonus: colored feedback for answers
Want the correct answer to flash green? Adjust answer so it colors the buttons first, then shows the next question after a second:
function answer(index) {
const buttons = choicesEl.querySelectorAll("button");
const correct = questions[questionNumber].correct;
buttons[correct].style.backgroundColor = "#16a34a";
if (index !== correct) {
buttons[index].style.backgroundColor = "#dc2626";
} else {
points++;
}
setTimeout(() => {
questionNumber++;
if (questionNumber < questions.length) {
showQuestion();
} else {
questionEl.textContent = "Done! 🎉";
choicesEl.innerHTML = "";
statusEl.textContent = "You scored " + points + " out of " + questions.length + " points.";
}
}, 1000);
}
setTimeout(() => {...}, 1000) means “run this after 1000 milliseconds.” And style.backgroundColor changes CSS directly from JavaScript.
Check yourself: The quiz asks questions one after another, keeps score, and shows the result at the end. You just wrote your first real application: it has data, logic, and a user interface.
What to take away from this lesson
- Keep data (an array of objects) separate from logic (functions), then swapping the questions is all it takes to get a new quiz.
- JavaScript can create elements:
createElement+appendChild. setTimeoutruns code after a delay.
© 2026 Ing. Martin Polak / AlgoRhino · Content usage terms