π οΈ Section project: the quiz app
Section 8: Objects and timeLesson 8.3 Β· β± 14 min
π― Project goal
Questions appear one after the other, every question has answer buttons, and at the end the score appears.

- An array of objects: every question has its text, its answers and the number of the right answer
right. forEach(function (text, k) β¦)gives us the answer and its number together.- After the last question the
finishfunction runs.
HTML
<div class="q">
<p id="question"></p>
<div id="answers"></div>
<p id="score"></p>
</div>
CSS
body { font-family: Tahoma, sans-serif; }
.q { max-width: 420px; margin: auto; text-align: center; }
#question { font-size: 24px; }
#answers button { display: block; width: 100%; margin: 6px 0; padding: 10px; font-size: 18px; border-radius: 12px; border: 2px solid #2557c9; background: white; }
#score { font-size: 22px; font-weight: bold; color: #1a8a34; }
JavaScript
const questions = [
{ q: "What is the capital of Egypt?", answers: ["Cairo", "Giza", "Aswan"], right: 0 },
{ q: "5 Γ 6 = ?", answers: ["11", "30", "56"], right: 1 },
{ q: "Which one is a planet?", answers: ["The Moon", "The Sun", "Mars"], right: 2 },
];
let i = 0;
let score = 0;
function show() {
const item = questions[i];
document.getElementById("question").textContent = (i + 1) + ". " + item.q;
const box = document.getElementById("answers");
box.innerHTML = "";
item.answers.forEach(function (text, k) {
const b = document.createElement("button");
b.textContent = text;
b.onclick = function () {
if (k === item.right) score++;
i++;
if (i < questions.length) show();
else finish();
};
box.appendChild(b);
});
}
function finish() {
document.getElementById("question").textContent = "Finished! π";
document.getElementById("answers").innerHTML = "";
document.getElementById("score").textContent = "Score: " + score + " / " + questions.length;
}
show();
π₯οΈ The page in Robo Web:

π₯οΈ What happens on the stage?
We answered: Cairo β, then 11 β, then Mars β, and the score was 2 / 3.
- Add two new questions.
- Colour the button green for the right answer and red for a wrong one before moving on.
π Another example: a quiz with two questions and a message for each answer
After every answer β or β appears in the Console, then the next question.
HTML
<p id="q"></p>
<div id="answers"></div>
<p id="score"></p>
CSS
body { font-family: Tahoma, sans-serif; }
#q { font-size: 22px; }
#answers button { display: block; width: 100%; margin: 6px 0; padding: 8px; font-size: 18px; }
#score { font-size: 20px; font-weight: bold; }
JavaScript
const questions = [
{ q: "2 + 3 = ?", answers: ["4", "5", "6"], right: 1 },
{ q: "Biggest planet?", answers: ["Jupiter", "The Earth"], right: 0 },
];
let i = 0, score = 0;
function show() {
const item = questions[i];
document.getElementById("q").textContent = item.q;
const box = document.getElementById("answers");
box.innerHTML = "";
item.answers.forEach(function (text, k) {
const b = document.createElement("button");
b.textContent = text;
b.onclick = function () {
if (k === item.right) { score++; console.log("β
"); }
else console.log("β The answer: " + item.answers[item.right]);
i++;
if (i < questions.length) show();
else {
document.getElementById("q").textContent = "Finished! π";
box.innerHTML = "";
document.getElementById("score").textContent = "Score: " + score + " / " + questions.length;
}
};
box.appendChild(b);
});
}
show();
π₯οΈ The page in Robo Web:

π₯οΈ The Console in Robo Web:
β β The answer: Jupiter
βοΈ Practise
Try the example here: copy the code into its tabs, change it, and watch the page change.
βοΈ Practise here in the lessonβ Close the labor open the lab in a new page β
π― Challenge yourself
Easy
One question with three answers: the right one writes βRight β β.
π Show the solutionπ Hide the solution
HTML
<p id="q"></p>
<div id="answers"></div>
<p id="score"></p>
CSS
body { font-family: Tahoma, sans-serif; }
#q { font-size: 22px; }
#answers button { display: block; width: 100%; margin: 6px 0; padding: 8px; font-size: 18px; }
#score { font-size: 20px; font-weight: bold; }
JavaScript
const answers = ["Cairo", "Paris", "Rome"];
document.getElementById("q").textContent = "Capital of Egypt?";
answers.forEach(function (text) {
const b = document.createElement("button");
b.textContent = text;
b.onclick = function () {
document.getElementById("score").textContent = text === "Cairo" ? "Right β
" : "Wrong β";
};
document.getElementById("answers").appendChild(b);
});
π₯οΈ The page in Robo Web:

Result: Right β
Medium
The right button turns green, and the wrong one red.
π Show the solutionπ Hide the solution
HTML
<p id="q"></p>
<div id="answers"></div>
<p id="score"></p>
CSS
body { font-family: Tahoma, sans-serif; }
#q { font-size: 22px; }
#answers button { display: block; width: 100%; margin: 6px 0; padding: 8px; font-size: 18px; }
#score { font-size: 20px; font-weight: bold; }
JavaScript
const questions = [
{ q: "2 + 3 = ?", answers: ["4", "5", "6"], right: 1 },
{ q: "Biggest planet?", answers: ["Jupiter", "The Earth"], right: 0 },
];
let i = 0, score = 0;
function show() {
const item = questions[i];
document.getElementById("q").textContent = item.q;
const box = document.getElementById("answers");
box.innerHTML = "";
item.answers.forEach(function (text, k) {
const b = document.createElement("button");
b.textContent = text;
b.onclick = function () {
b.style.background = k === item.right ? "lightgreen" : "salmon";
};
box.appendChild(b);
});
}
show();
π₯οΈ The page in Robo Web:

Result: the button is red.
Hard
At the end: βExcellentβ if all the answers were right.
π Show the solutionπ Hide the solution
HTML
<p id="q"></p>
<div id="answers"></div>
<p id="score"></p>
CSS
body { font-family: Tahoma, sans-serif; }
#q { font-size: 22px; }
#answers button { display: block; width: 100%; margin: 6px 0; padding: 8px; font-size: 18px; }
#score { font-size: 20px; font-weight: bold; }
JavaScript
const questions = [
{ q: "2 + 3 = ?", answers: ["4", "5", "6"], right: 1 },
{ q: "Biggest planet?", answers: ["Jupiter", "The Earth"], right: 0 },
];
let i = 0, score = 0;
function show() {
const item = questions[i];
document.getElementById("q").textContent = item.q;
const box = document.getElementById("answers");
box.innerHTML = "";
item.answers.forEach(function (text, k) {
const b = document.createElement("button");
b.textContent = text;
b.onclick = function () {
if (k === item.right) score++;
i++;
if (i < questions.length) show();
else {
document.getElementById("q").textContent = "Finished! π";
box.innerHTML = "";
document.getElementById("score").textContent = "Score: " + score + " / " + questions.length;
if (score === questions.length) document.getElementById("score").textContent += " Excellent π";
}
};
box.appendChild(b);
});
}
show();
π₯οΈ The page in Robo Web:

Result: Score: 2 / 2 Excellent π

