كيف سيعمل
ستبني اختباراً: سؤال، ثلاث خيارات، انقر للإجابة، والنتيجة تظهر في النهاية. بيانات الاختبار (أسئلة وإجابات) ستعيش في مصفوفة 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,
},
];
- Array
[...]قائمة قيم – تعرفها من قبل (photosكان array أيضاً). - Object
{...}صندوق بأقسام مسماة:text،choices،correct. correct: 1يعني: الخيار رقم 1 صحيح – العد يبدأ من صفر، إذن الثاني بالترتيب.
شاهد معي: محرك الاختبار
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();
جديد:
document.createElement("button")– JavaScript لا يجد عناصر فقط، بل ينشئها. ننشئ زراً لكل خيار.appendChild(...)– أدرج العنصر المنشأ في الصفحة.innerHTML = ""– أفرغ محتوى عنصر (نمسح الأزرار القديمة قبل السؤال التالي).- Functionان يمرّران العمل لبعضهما:
showQuestion()يرسم،answer()يقيّم. تقسيم البرنامج لfunctions صغيرة عادة المحترفين.
دورك الآن 💪
ابنِ اختباراً بأسئلتك – خمسة على الأقل. أفكار:
- 🏫 اختبار عن مدرستنا – كم عمر المدرسة؟ من المدير؟ كم صفاً لدينا؟
- 🙋 إلى أي مدى تعرفني؟ – طعامي المفضل؟ كم عمري؟ اسم كلبي؟
- 🌍 اختبار سفر – عاصمة كرواتيا؟ أعلى جبل في بلدك؟ أي اتجاه البحر؟
- ⚽ إلى أي مدى تعرف فريقنا؟ – من القائد؟ كم هدف سجّل Tom؟ ألوان النادي؟
فقط بدّل محتوى 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.
تحقق من نفسك: الاختبار يسأل أسئلة واحداً تلو الآخر، يحتفظ بالنتيجة، ويعرضها في النهاية. كتبت للتو أول تطبيق حقيقي – له بيانات، منطق، وواجهة مستخدم.
ما ستخرج به من هذا الدرس
- افصل البيانات (array of objects) عن المنطق (functions) – ثم تبديل الأسئلة يكفي لاختبار جديد.
- JavaScript ينشئ عناصر:
createElement+appendChild. setTimeoutيشغّل كوداً بعد تأخير.
© 2026 Ing. Martin Polak / AlgoRhino · شروط استخدام المحتوى