Ψ§Ω†ΨͺΩ‚Ω„ Ω„Ω„Ω…Ψ­ΨͺΩˆΩ‰
Robo EgyptRobo Egypt
☰ Course contents
Robo Web: home
0. Lab tour1
1. HTML: the page's content7
2. CSS: the page's look6
3. JavaScript: the actions7
4. The project: the to-do list3
5. Pro CSS: shadows and movement5
6. Arrays and building the page with code3
7. Events and forms3
8. Objects and time3
9. Canvas: drawing and games3
10. The final project: the memory game3

πŸ› οΈ 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.
The first question in the lab
The first question in the lab
  • 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 finish function 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:The page in Robo Web
πŸ–₯️ What happens on the stage?
We answered: Cairo βœ“, then 11 βœ—, then Mars βœ“, and the score was 2 / 3.
  1. Add two new questions.
  2. 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.

🌐 A quiz with two questions and a message for each answer
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 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 lessonor open the lab in a new page β†—

🎯 Challenge yourself

Easy

One question with three answers: the right one writes β€œRight βœ…β€.

πŸ‘€ Show the solution
🌐 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:The page in Robo Web

Result: Right βœ…

Medium

The right button turns green, and the wrong one red.

πŸ‘€ Show the solution
🌐 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:The page in Robo Web

Result: the button is red.

Hard

At the end: β€œExcellent” if all the answers were right.

πŸ‘€ Show the solution
🌐 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:The page in Robo Web

Result: Score: 2 / 2 Excellent πŸ†

Finished the lesson? Mark it so your progress counts.