مشروع اختبار تفاعلي بالجافاسكريبت: الكود كامل + شرح

مشروع اختبار تفاعلي بالجافاسكريبت: الكود كامل + شرح

غلاف مشروع اختبار تفاعلي بالجافاسكريبت

مشروع 3: اختبار تفاعلي بجافاسكريبت من الصفر

بيانات الأسئلة، عرض الاختيارات، قفل الإجابة بعد الاختيار، تفسير الصح، نتيجة نهائية وإعادة بداية.

جرّب تجاوب غلط، واضغط أكتر من مرة، وبعدها أعد الاختبار. النتيجة لازم تبدأ من جديد.

تنبيه: اختبار تعليمي محلي، مش منصة امتحانات رسمية، والإجابات ظاهرة في المصدر.

إيه اللي هتتعلمه في المشروع ده

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

إزاي الكود شغال

كل سؤال هو object فيه النص والاختيارات ورقم الإجابة الصحيحة answer والشرح. دالة render بتبني أزرار الاختيارات من المصفوفة، ودالة answer بتقارن الاختيار بالإجابة الصحيحة وتزوّد score وتلوّن الزرار. المتغير locked بيمنع الضغط مرة تانية على نفس السؤال، وزرار التالي مبيشتغلش غير بعد الإجابة. والنصوص بتتعرض بـ textContent مش innerHTML.

غلطة شائعة

إنك تسيب المستخدم يضغط على أكتر من اختيار في نفس السؤال، فالنتيجة تتحسب مرتين أو أكتر. لو شلت locked وجرّب تضغط كذا مرة هتشوف المشكلة بنفسك.

تمرين تكمل بيه

ضيف سؤال رابع في المصفوفة من غير ما تغيّر باقي الكود، وشوف إن عدد الأسئلة في النتيجة بيتغيّر لوحده. وبعدها جرّب تخلّي ترتيب الأسئلة عشوائي.

طريقة التشغيل

  1. اعمل مجلد جديد وسمّه project3.
  2. انسخ كل ملف من الأسفل واحفظه بنفس اسمه داخل المجلد.
  3. افتح index.html في متصفح حديث. أو شغّل خادم محلي من المجلد الذي يحتوي project3:
    python3 -m http.server 8000
    على ويندوز: py -m http.server 8000
    وافتح: http://localhost:8000/project3/

index.html

<!doctype html>
<html lang="ar" dir="rtl"><head><meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>اختبر معلوماتك | Ayoub EduTech</title><link rel="stylesheet" href="style.css">
<script src="app.js" defer></script></head><body><main>
<small>Ayoub EduTech • مشروع 03</small><h1>اختبر معلوماتك</h1>
<section><small id="progress"></small><h2 id="question"></h2>
<div id="choices"></div><p id="explanation"></p>
<button id="next" disabled>السؤال التالي</button><button id="restart">ابدأ من جديد</button>
</section>
<p id="status" role="status" aria-live="polite"></p>
<small>مشروع تعليمي محلي • JavaScript</small></main></body></html>

style.css

* {
  box-sizing:border-box
}
body {
  margin:0;
  background:#140b26;
  color:#f7f2ff;
  font:18px "Noto Sans Arabic",Arial;
  direction:rtl
}
main {
  max-width:740px;
  margin:32px auto;
  padding:24px
}
h1 {
  font-size:30px;
  margin:8px 0
}
small {
  color:#c5b2de
}
section,article {
  background:#24133d;
  border:1px solid #614480;
  border-radius:18px;
  padding:20px;
  margin:18px 0
}
label {
  display:block;
  margin:12px 0
}
input,select,button {
  font:inherit;
  border-radius:10px;
  padding:10px;
  border:1px solid #8867a7
}
input,select {
  background:#10091b;
  color:white;
  width:100%
}
button {
  background:#f5ca52;
  color:#211234;
  cursor:pointer;
  margin:5px
}
button:disabled {
  opacity:.45;
  cursor:not-allowed
}
button:focus-visible,input:focus-visible,select:focus-visible {
  outline:3px solid #68e1d2
}
output,strong {
  color:#7ce6d5
}
.row {
  display:flex;
  gap:10px;
  align-items:center;
  justify-content:space-between
}
.row>* {
  min-width:0
}
ul {
  padding:0;
  list-style:none
}
li {
  padding:12px 0;
  border-bottom:1px solid #59416c
}
.error {
  color:#ffb6bb
}
#status {
  min-height:32px
}
#clock {
  font:64px monospace;
  text-align:center;
  direction:ltr
}
#choices button {
  display:block;
  width:100%;
  text-align:right
}
#choices button.correct {
  background:#68d7b8
}
#choices button.wrong {
  background:#ee9ca9
}
#products article {
  margin:10px 0
}
a {
  color:#7ce6d5
}
@media(max-width:500px) {
  main {
    padding:16px;
    margin:10px auto
  }
  .row {
    flex-wrap:wrap
  }
  h1 {
    font-size:26px
  }
}

app.js

const $ = id => document.getElementById(id);
const questions = [
  { text: 'أي كلمة تعرف قيمة لا يعاد إسنادها؟',
    choices: ['const', 'let', 'var'], answer: 0,
    explanation: 'const تمنع إعادة الإسناد، لكنها لا تجمد محتوى object.' },
  { text: 'أي دالة تحول نص JSON إلى قيمة؟',
    choices: ['JSON.stringify', 'JSON.parse', 'Array.map'], answer: 1,
    explanation: 'JSON.parse تقرأ نص JSON، وقد ترمي خطأ لو النص تالف.' },
  { text: 'أي خاصية تعرض نص المستخدم بدون تفسيره كـ HTML؟',
    choices: ['innerHTML', 'outerHTML', 'textContent'], answer: 2,
    explanation: 'textContent تعرض النص كما هو، بدون تنفيذ وسوم HTML.' }
];
let index = 0;
let score = 0;
let locked = false;
function render() {
  locked = false;
  const q = questions[index];
  $('progress').textContent = `السؤال ${index + 1} من ${questions.length}`;
  $('question').textContent = q.text;
  $('explanation').textContent = '';
  $('choices').replaceChildren(); $('next').disabled = true;
  $('next').hidden = false;
  $('next').textContent = index === questions.length - 1 ? 'عرض النتيجة' : 'السؤال التالي';
  q.choices.forEach((choice, choiceIndex) => {
    const button = document.createElement('button');
    button.textContent = choice;
    button.addEventListener('click', () => answer(choiceIndex));
    $('choices').append(button);
  });
}
function answer(selected) {
  if (locked) return;
  locked = true;
  const q = questions[index];
  if (selected === q.answer) score++;
  [...$('choices').children].forEach((button, i) => {
    button.disabled = true;
    if (i === q.answer) button.className = 'correct';
    else if (i === selected) button.className = 'wrong';
  });
  $('explanation').textContent = q.explanation;
  $('next').disabled = false;
}
$('next').addEventListener('click', () => {
  if (!locked) return;
  if (index + 1 < questions.length) { index++; render(); return; }
  $('question').textContent = `نتيجتك ${score} من ${questions.length}`;
  $('progress').textContent = 'اكتمل الاختبار';
  $('choices').replaceChildren(); $('explanation').textContent = 'راجع الشرح وجرب مرة ثانية.';
  $('next').hidden = true;
});
$('restart').addEventListener('click', () => {
  index = 0; score = 0; $('status').textContent = ''; render();
});
render();

تحميل المشاريع

تحميل ملف ZIP للمشاريع الخمسة (Google Drive)

تابع Ayoub EduTech وطبّق بإيدك.