← المستوى 2 – الساحر

الدرس 6 من 8 ⏱ 40 دقيقة

اختبار لزوارك

أسئلة، إجابات، واحتفاظ بالنتيجة. بصورة مصغّرة، تتعلّم منطق كل لعبة.

كيف سيعمل

ستبني اختباراً: سؤال، ثلاث خيارات، انقر للإجابة، والنتيجة تظهر في النهاية. بيانات الاختبار (أسئلة وإجابات) ستعيش في مصفوفة objects – يبدو fancy، لكنها مجرد قائمة منظمة.

شاهد معي: بيانات الاختبار

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,
  },
];

شاهد معي: محرك الاختبار

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();

جديد:

دورك الآن 💪

ابنِ اختباراً بأسئلتك – خمسة على الأقل. أفكار:

فقط بدّل محتوى array questions – محرك الاختبار يبقى نفسه. لهذا البيانات والمنطق منفصلان!

إضافة: ردود ملونة للإجابات

تريد الإجابة الصحيحة تومض بالأخضر؟ عدّل answer لتلوّن الأزرار أولاً، ثم تعرض السؤال التالي بعد ثانية:

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) يعني “شغّل هذا بعد 1000 millisecond.” وstyle.backgroundColor يغيّر CSS مباشرة من JavaScript.

✅

تحقق من نفسك: الاختبار يسأل أسئلة واحداً تلو الآخر، يحتفظ بالنتيجة، ويعرضها في النهاية. كتبت للتو أول تطبيق حقيقي – له بيانات، منطق، وواجهة مستخدم.

ما ستخرج به من هذا الدرس