الخطوة 1
حالة البداية
اكتب startQuiz() بترجّع { index: 0, score: 0 }.
function startQuiz() {
// اكتب هنا
}
الخطوة 2
جاوب
اكتب answer(state, questions, choice): كل سؤال شكله { q, options, correct } (correct هو رقم الاختيار الصح). بترجّع حالة جديدة: لو choice صح الدرجة تزيد 1، وفي الحالتين index يزيد 1. الحالة الأصلية ميتغيّرش فيها حاجة.
function startQuiz() {
return { index: 0, score: 0 };
}
function answer(state, questions, choice) {
// اكتب هنا
}
الخطوة 3
خلص الاختبار؟
اكتب isFinished(state, questions) بترجّع true لو الأسئلة كلها اتجاوبت.
function startQuiz() {
return { index: 0, score: 0 };
}
function answer(state, questions, choice) {
const right = questions[state.index].correct === choice;
return { index: state.index + 1, score: state.score + (right ? 1 : 0) };
}
function isFinished(state, questions) {
// اكتب هنا
}
الخطوة 4
النتيجة والتقدير
اكتب result(state, questions) بترجّع { percent, label }: النسبة المئوية مقرّبة لأقرب رقم صحيح، والتقدير: من 80 'ممتاز'، من 50 'كويس'، وغير كده 'حاول تاني'.
function startQuiz() {
return { index: 0, score: 0 };
}
function answer(state, questions, choice) {
const right = questions[state.index].correct === choice;
return { index: state.index + 1, score: state.score + (right ? 1 : 0) };
}
function isFinished(state, questions) {
return state.index >= questions.length;
}
function result(state, questions) {
// اكتب هنا
}