ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Quiz App ဆိုတာ မေးခွန်းကတ်ပြားတစ်ထပ်ကို တစ်ကတ်ချင်း လှန်ပြတဲ့ ဆရာတစ်ယောက်နဲ့ တူပါတယ်။ ဆရာက ဘယ်ကတ်ရောက်နေလဲ၊ ကျောင်းသားက ဒီကတ်ကို ဖြေပြီးပြီလား၊ အမှတ်ဘယ်လောက် ရပြီလဲ ဆိုတာကို မှတ်ထားရပါတယ်။ ကျောင်းသားက ဖြေပြီးမှ နောက်ကတ်ကို လှန်ပြီး ကတ်ကုန်သွားရင် ရလဒ်ကို ကြေညာပါတယ်။
JavaScript မှာ ဒီ "ဆရာ့မှတ်စု" က state object တစ်ခုပါ။ Button တွေ၊ စာသားတွေက state ကို ကြည့်ပြီး ဆွဲပြတဲ့ ပုံတွေ သက်သက်ပါပဲ။ စည်းမျဉ်း (ဘယ်အချိန် အမှတ်ပေးမလဲ၊ ဘယ်အချိန် ရှေ့ဆက်ခွင့်ပြုမလဲ) ကို state ဘက်မှာပဲ ရေးပါတယ်။
Problem → Why → Solution
ပြဿနာ: click handler ထဲမှာ အားလုံး ရောနေတာ
အောက်က draft မှာ score ရေတွက်တာ၊ နောက်မေးခွန်းကို သွားတာ၊ button ဆွဲတာ အားလုံး click handler တစ်ခုထဲ ရောနေပါတယ်။ စမ်းကြည့်တဲ့အခါ အလုပ်လုပ်သလို ထင်ရပေမယ့် bug လေးခု ပုန်းနေပါတယ်။
User က အဖြေမှန် button ကို မြန်မြန် နှစ်ခါ နှိပ်ရင် score နှစ်ခါ တိုးပြီး မေးခွန်းတစ်ခုလည်း ကျော်သွားပါတယ်။ data-correct="true" ဆိုတဲ့ attribute ကြောင့် right-click နှိပ်ပြီး Inspect လုပ်ရုံနဲ့ အဖြေမှန်ကို မြင်ရပါတယ်။ နောက်ဆုံးမေးခွန်းပြီးရင် questions[3] က undefined ဖြစ်လို့ render() ထဲမှာ crash ဖြစ်ပါတယ်။ Choice စာသားကို innerHTML နဲ့ ထည့်ထားလို့ "<img onerror=...>" လို စာသားပါရင် HTML အဖြစ် run သွားနိုင်ပါတယ်။
let score = 0;
let index = 0;
function render(q) {
quizEl.innerHTML = q.choices
.map((c, i) => `<button data-correct="${i === q.correctIndex}">${c}</button>`)
.join(""); // "Inspect element" shows which button is correct
}
quizEl.addEventListener("click", (event) => {
if (event.target.dataset.correct === "true") score++; // double click = 2 points
index++;
render(questions[index]); // after the last one: questions[3] is undefined
});ဘာကြောင့် ဒီ bug တွေ ဖြစ်တာလဲ
Quiz ရဲ့ စည်းမျဉ်းတွေကို ဘယ်နေရာမှာမှ ရှင်းရှင်း မရေးထားလို့ပါ။ "ဖြေပြီးသား မေးခွန်းကို ထပ်ဖြေလို့ မရ"၊ "မဖြေရသေးရင် ရှေ့မဆက်ရ"၊ "မေးခွန်းကုန်ရင် ရလဒ်ပြ" ဆိုတဲ့ rule တွေက state ထဲမှာ မရှိတဲ့အတွက် click တိုင်းက ဘာမဆို ဖြစ်စေနိုင်ပါတယ်။
Core concept: quiz ဆိုတာ state machine သေးသေးလေးပါ
State machine ဆိုတာ အခြေအနေ (state) အနည်းငယ်ပဲ ရှိပြီး သတ်မှတ်ထားတဲ့ action တွေကနေသာ state တစ်ခုကနေ တစ်ခုကို ကူးလို့ရတဲ့ system ပါ။ ဒီ quiz မှာ state ကို value လေးခုနဲ့ ဖော်ပြနိုင်ပါတယ်။ index (ဘယ်မေးခွန်းလဲ)၊ score၊ answered (ဒီမေးခွန်းကို ဖြေပြီးပြီလား) နဲ့ history (ဘာတွေ ဖြေခဲ့လဲ) ပါ။ Finished ဖြစ်မဖြစ်ကိုတော့ သီးသန့် သိမ်းစရာ မလိုပါဘူး၊ index >= items.length ကနေ တွက်ယူလို့ ရပါတယ်။
| Action | ခွင့်ပြုတဲ့အချိန် | State ပြောင်းလဲမှု |
|---|---|---|
| submit(choice) | answered က false ဖြစ်ပြီး မပြီးသေးချိန် | score တိုး (မှန်ရင်)၊ answered = true၊ history ထဲ ထည့် |
| next() | answered က true ဖြစ်ချိန် | index++၊ answered = false |
| results() | အချိန်မရွေး | မပြောင်းပါ၊ ဖတ်ရုံသာ |
Table ထဲက "ခွင့်ပြုတဲ့အချိန်" column ဟာ problem section ထဲက bug တွေကို ဖြေရှင်းတဲ့ အပိုင်းပါ။ submit() က answered ဖြစ်ပြီးသားဆိုရင် ဘာမှမလုပ်ဘဲ ပြန်ထွက်လို့ double click က အမှတ်နှစ်ခါ မပေးတော့ပါဘူး။ next() က answered မဖြစ်သေးရင် ငြင်းလို့ မဖြေဘဲ ကျော်လို့ မရပါဘူး။ isFinished() ကို render မလုပ်ခင် စစ်လို့ undefined မေးခွန်းကို ဆွဲဖို့ မကြိုးစားတော့ပါဘူး။
အဖြေကို index (correctIndex: 1) နဲ့ မသိမ်းဘဲ value (answer: "const") နဲ့ သိမ်းပါတယ်။ ဒါမှ choice တွေရဲ့ အစီအစဉ်ကို ပြောင်းလိုက်ရင်တောင် အဖြေစစ်တာ မှန်နေပါတယ်။ နောက်ဆုံးအနေနဲ့ percent ကို number အဖြစ် ထားပြီး grade ကို ဆုံးဖြတ်ပါတယ်။ toFixed(1) က string ကို ပြန်ပေးလို့ ပြခါနီးမှသာ သုံးပါတယ်။
State ထဲမှာ မပါတဲ့အရာတွေကိုလည်း ကြည့်ပါ
State ထဲမှာ button element၊ CSS class၊ innerHTML တစ်ခုမှ မပါပါဘူး။ DOM ကို အပြင်မှာ ထားလို့ createQuiz() တစ်ခုတည်းကို web page၊ terminal version နဲ့ test သုံးမျိုးလုံးက current() နဲ့ results() ကို ဖတ်ပြီး သုံးနိုင်ပါတယ်။ Feature အသစ်ထည့်တာလည်း လွယ်ပါတယ်။ Timer၊ "skip" rule ဒါမှမဟုတ် "မှားတာတွေကို ပြန်ဖြေ" mode ဆိုတာ click handler ထဲမှာ if တွေ ထပ်ရှုပ်တာ မဟုတ်တော့ဘဲ အပေါ်က table ထဲမှာ guard ပါတဲ့ action တစ်ကြောင်း ထပ်တိုးရုံပါပဲ။ ဘယ် action က ဘယ်အချိန် ခွင့်ပြုလဲ ဆိုတာ table မှာ ရေးနိုင်ရင် code ရေးဖို့ အဆင်သင့် ဖြစ်ပါပြီ။
- Question data က ကတ်ပြားတွေ
- State object က ဆရာ့မှတ်စု
- submit/next က စည်းမျဉ်းပါတဲ့ တံခါး
- UI က မှတ်စုကို ကြည့်ပြီး ဆွဲတာပဲ
UI က score ကို ကိုယ်တိုင် မတိုးပါဘူး။ submit() ကို ခေါ်ပြီး ပြန်ရတဲ့ message နဲ့ state ကို ပြရုံပါပဲ။
အခြေခံ ပုံစံ
function createFlow(items) {
const state = { index: 0, answered: false };
return {
current: () => items[state.index],
submit(input) { if (state.answered) return; state.answered = true; /* score */ },
next() { if (state.answered) { state.index++; state.answered = false; } },
isFinished: () => state.index >= items.length,
};
}Factory function က state ကို closure ထဲ ဝှက်ထားပြီး စည်းမျဉ်းပါတဲ့ method တွေကိုပဲ ထုတ်ပေးပါတယ်။ Method တစ်ခုစီက လုပ်ခွင့်ရှိမရှိ အရင်စစ်ပြီးမှ state ကို ပြောင်းပါတယ်။
Quiz logic ကို double click နဲ့ စမ်းကြည့်မယ်
ဒီ file က DOM ကို လုံးဝ မထိပါဘူး။ Loop က user ရဲ့ click တွေကို အတုလုပ်ပြီး ပထမမေးခွန်းမှာ double click တစ်ခါ ထည့်ထားပါတယ်။ Output မှာ ဒုတိယ click ကို လက်မခံဘဲ "Ignored" ဖြစ်သွားတာနဲ့ နောက်ဆုံး score က 2/3 ပဲ ဖြစ်နေတာကို စစ်ကြည့်ပါ။
// quiz.js - quiz rules with no DOM. The page renders whatever state says.
const questions = [
{ id: "q1", text: "What does JS stand for?",
choices: ["Java Source", "JavaScript", "JSON"], answer: "JavaScript" },
{ id: "q2", text: "Which keyword declares a constant?",
choices: ["let", "var", "const"], answer: "const" },
{ id: "q3", text: "What is 2 + 2?", choices: ["4", "22", "5"], answer: "4" },
];
function createQuiz(items) {
const state = { index: 0, score: 0, answered: false, history: [] };
function current() {
return items[state.index];
}
function submit(choice) {
if (state.answered || isFinished()) return "Ignored: already answered";
const q = current();
const correct = choice === q.answer;
if (correct) state.score++;
state.answered = true;
state.history.push({ id: q.id, choice, correct });
return correct ? "Correct!" : `Wrong. The answer is ${q.answer}`;
}
function next() {
if (!state.answered) return false; // must answer before moving on
state.index++;
state.answered = false;
return true;
}
function isFinished() {
return state.index >= items.length;
}
function results() {
const percent = (state.score / items.length) * 100; // a number, not a string
let grade = "Needs practice";
if (percent >= 90) grade = "Excellent";
else if (percent >= 70) grade = "Good";
else if (percent >= 50) grade = "Pass";
const shown = percent.toFixed(1); // format only for display
return { score: state.score, total: items.length, percent: shown, grade };
}
return { current, submit, next, isFinished, results };
}
const quiz = createQuiz(questions);
const picks = ["JavaScript", "let", "4"];
for (const pick of picks) {
console.log(`${quiz.current().text} -> ${pick}`);
console.log(" " + quiz.submit(pick));
if (pick === "JavaScript") console.log(" " + quiz.submit(pick)); // a double click
quiz.next();
}
const r = quiz.results();
console.log(`Score: ${r.score}/${r.total} (${r.percent}%) - ${r.grade}`);Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- All state in one place
const state = { index: 0, score: 0, answered: false, history: [] };State ကို createQuiz() ရဲ့ closure ထဲမှာ ထားလို့ အပြင်က code က state.score = 100 လို့ ရေးလို့ မရပါဘူး။ ပြောင်းချင်ရင် submit() နဲ့ next() ကိုပဲ ဖြတ်ရပါမယ်။
- The double-click guard
if (state.answered || isFinished()) return "Ignored: already answered";ပထမ click က answered ကို true ဖြစ်စေပါတယ်။ ဒုတိယ click ရောက်လာတဲ့အခါ ဒီ line မှာ ပြန်ထွက်သွားလို့ score မတိုးတော့ပါဘူး။
- Grade by value
const correct = choice === q.answer;Button ရဲ့ နေရာ (index) ကို မနှိုင်းယှဉ်ဘဲ choice စာသားကို answer နဲ့ နှိုင်းယှဉ်ပါတယ်။ Choice အစီအစဉ် ပြောင်းလည်း ဒီ line ကို ပြင်စရာ မလိုပါဘူး။
- No skipping
if (!state.answered) return false; // must answer before moving onမဖြေရသေးတဲ့ မေးခွန်းကနေ next() ကို ခေါ်ရင် false ပြန်ပြီး index မပြောင်းပါဘူး။ UI က "Next" button ကို disable လုပ်ထားဖို့ ဒီ return value ကို သုံးနိုင်ပါတယ်။
- Decide with numbers, format at the end
const shown = percent.toFixed(1); // format only for displayGrade ကို 66.666... ဆိုတဲ့ number နဲ့ ဆုံးဖြတ်ပြီးမှ "66.7" string ကို ထုတ်ပါတယ်။ String ကို >= 90 နဲ့ နှိုင်းယှဉ်ရင် JavaScript က coercion နဲ့ အဆင်ပြေအောင် လုပ်ပေးပေမယ့် ဒါက မတော်တဆ အလုပ်ဖြစ်နေတာပဲ ဖြစ်ပါတယ်။
What does JS stand for? -> JavaScript
Correct!
Ignored: already answered
Which keyword declares a constant? -> let
Wrong. The answer is const
What is 2 + 2? -> 4
Correct!
Score: 2/3 (66.7%) - PassPage နဲ့ ချိတ်ဆက်ခြင်း
Browser ဘက်က code က state ကို ဆွဲပြရုံပါပဲ။ Choice စာသားကို textContent နဲ့ ထည့်လို့ HTML အဖြစ် မ run နိုင်တော့ပါဘူး။ Button ပေါ်မှာ အဖြေမှန်ကို ဖော်ပြတဲ့ attribute လည်း မရှိတော့ပါဘူး။ Click တစ်ခါပြီးတာနဲ့ button အားလုံးကို disable လုပ်တာက UI ဘက်က ကာကွယ်မှုဖြစ်ပြီး submit() ထဲက guard ကတော့ logic ဘက်က ကာကွယ်မှုပါ။
သတိပြုရမှာက questions array တစ်ခုလုံး browser ထဲ ရောက်နေတာပါ။ Source ကို ဖတ်တတ်တဲ့သူက အဖြေတွေကို တွေ့နိုင်ပါတယ်။ လေ့ကျင့်ခန်း quiz အတွက် ပြဿနာ မရှိပေမယ့် အမှတ်တကယ်ရေးတဲ့ စာမေးပွဲဆိုရင် အဖြေကို server ပေါ်မှာ စစ်ရပါမယ်။
// ui.js - browser only: draws the quiz state; it never decides the score
const box = document.querySelector("#quiz");
const feedback = document.querySelector("#feedback");
const quiz = createQuiz(questions);
function render() {
if (quiz.isFinished()) {
const r = quiz.results();
box.textContent = `Score: ${r.score}/${r.total} (${r.percent}%) - ${r.grade}`;
return;
}
const q = quiz.current();
const buttons = q.choices.map((choice) => {
const button = document.createElement("button");
button.textContent = choice; // plain text: no HTML injection, no hidden answer
button.addEventListener("click", () => {
feedback.textContent = quiz.submit(choice);
buttons.forEach((b) => (b.disabled = true));
setTimeout(() => {
quiz.next();
feedback.textContent = "";
render();
}, 800);
});
return button;
});
box.replaceChildren(document.createTextNode(q.text), ...buttons);
}
render();လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
Language learning app တွေ၊ company onboarding quiz တွေ၊ flashcard app တွေ အားလုံး ဒီ index/score/answered ပုံစံကို သုံးကြပါတယ်။ Logic က pure function တွေ ဖြစ်နေလို့ "double click က score နှစ်ခါ မတိုးရ"၊ "နောက်ဆုံးမေးခွန်းပြီးရင် finished ဖြစ်ရမယ်" လို rule တွေကို browser မဖွင့်ဘဲ unit test နဲ့ စစ်နိုင်ပါတယ်။
Accessibility ကိုလည်း စဉ်းစားရပါမယ်။ Choice တွေကို fieldset နဲ့ legend ထဲက radio button တွေအဖြစ် ထားရင် keyboard နဲ့ arrow key သုံးပြီး ရွေးလို့ရပြီး screen reader ကလည်း မေးခွန်းကို ဖတ်ပေးပါတယ်။ "Correct!" လို feedback ကို aria-live="polite" ပါတဲ့ element ထဲ ထည့်ရင် screen reader က ကြေညာပေးပါတယ်။ Timer ပါတဲ့ quiz ဆိုရင် quiz ပြီးတာနဲ့ clearInterval() ခေါ်ဖို့ မမေ့ပါနဲ့။ မဟုတ်ရင် နောက်ကွယ်မှာ ဆက် run နေပြီး finished quiz ကို ပြင်နေနိုင်ပါတယ်။ Progress ကို localStorage ထဲ သိမ်းထားရင် page refresh လုပ်လည်း ဆက်ဖြေလို့ ရပါတယ်။
အမှားများ
ဖြစ်လေ့ရှိတဲ့ အမှားများ
Shuffle လုပ်ပြီးမှ correctIndex နဲ့ စစ်တာ
Choice တွေကို ရောမွှေလိုက်ပြီး correctIndex: 1 ကို မပြောင်းရင် index 1 နေရာမှာ အခု တခြား choice ရောက်နေပါပြီ။ အဖြေမှန်ရွေးတဲ့သူ အမှတ်မရဘဲ အမှားရွေးတဲ့သူ အမှတ်ရသွားနိုင်ပါတယ်။ အဖြေကို value နဲ့ သိမ်းပါ၊ ဒါမှမဟုတ် choice တစ်ခုစီကို id ပေးပါ။
Score ကို click handler ထဲမှာ တိုက်ရိုက် တိုးတာ
score++ ကို handler ထဲမှာ ရေးရင် click တိုင်းက အမှတ်ပေးပါတယ်။ Double click၊ keyboard Enter နှစ်ခါ၊ touch နဲ့ mouse event နှစ်ခု တပြိုင်နက် ဖြစ်တာတွေကြောင့် အမှတ် ပိုတိုးနိုင်ပါတယ်။ Score ကို guard ပါတဲ့ submit() ထဲမှာပဲ ပြောင်းပါ။
toFixed() ရလဒ်နဲ့ သင်္ချာ ဆက်တွက်တာ
toFixed() က string ပြန်ပေးပါတယ်။ "66.7" + 10 က "66.710" ဖြစ်သွားပါတယ်။ Grade၊ average၊ ranking တွက်တာတွေကို number နဲ့ပဲ လုပ်ပြီး screen ပေါ်တင်ခါနီးမှ format လုပ်ပါ။
Mini challenge
Choice တွေကို shuffle လုပ်ပြီး review ပြပါ
Choice တွေကို Fisher-Yates နည်းနဲ့ ရောမွှေတဲ့ shuffle()၊ question တစ်ခုကို shuffle လုပ်ပြီး copy အသစ် ပြန်ပေးတဲ့ prepareQuestion() နဲ့ မှားခဲ့တဲ့ မေးခွန်းတွေကို စာရင်းပြတဲ့ review() ကို ရေးပါ။ Seeded random ကို သုံးလို့ run တိုင်း order တူရပါမယ်။
- shuffle() က မူရင်း array ကို မပြောင်းဘဲ copy အသစ် ပြန်ပေးရပါမယ်
- Random ကို parameter အဖြစ် လက်ခံရပါမယ် (Math.random ကို တိုက်ရိုက် မခေါ်ရ)
- Shuffle လုပ်ပြီးနောက်မှာလည်း choice စာသားနဲ့ အဖြေမှန်မမှန် စစ်ရပါမယ်
- review() က မှားတဲ့ entry တစ်ခုစီအတွက် line တစ်ကြောင်း ပြန်ပေးရပါမယ်
// Given: a seeded generator, so the "random" order is the same on every run.
function seededRandom(seed) {
let s = seed;
return () => {
s = (s * 16807) % 2147483647;
return (s - 1) / 2147483646;
};
}
function shuffle(array, random) {
// TODO: copy the array first - the caller's array must keep its order
// TODO: Fisher-Yates: walk i from the last index down to 1,
// pick j = Math.floor(random() * (i + 1)), swap copy[i] and copy[j]
}
function prepareQuestion(question, random) {
// TODO: return a new question object whose choices are shuffled
}
function review(history, items) {
// TODO: for each wrong entry return "q2: you chose null, correct is object"
}
// TODO: shuffle a 3-question bank with seededRandom(42), print each order,
// score three clicks by choice text, then print the review linesရလဒ်: Seed 42 နဲ့ q1 ရဲ့ order က "unshift | shift | pop | push" ဖြစ်ပြီး score က 2/3၊ review line တစ်ကြောင်း (q2) ထွက်ပြီး မူရင်း q1 order က push,pop,shift,unshift အတိုင်း ကျန်နေရပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
Fisher-Yates မှာ i ကို နောက်ဆုံး index ကနေ 1 အထိ လျှော့ပြီး 0 ကနေ i အထိ (i ပါ) index j တစ်ခုကို ရွေးကာ [copy[i], copy[j]] = [copy[j], copy[i]] နဲ့ လဲပါ။ Review အတွက် history ကို filter() လုပ်ပြီး map() နဲ့ စာကြောင်း ပြောင်းပါ။
အဖြေ ကြည့်ရန်
// Seeded generator (Park-Miller): the same seed gives the same "random" order.
function seededRandom(seed) {
let s = seed;
return () => {
s = (s * 16807) % 2147483647;
return (s - 1) / 2147483646;
};
}
function shuffle(array, random) {
const copy = [...array]; // never reorder the caller's array
for (let i = copy.length - 1; i > 0; i--) {
const j = Math.floor(random() * (i + 1));
[copy[i], copy[j]] = [copy[j], copy[i]];
}
return copy;
}
function prepareQuestion(question, random) {
return { ...question, choices: shuffle(question.choices, random) };
}
function review(history, items) {
return history
.filter((h) => !h.correct)
.map((h) => {
const q = items.find((item) => item.id === h.id);
return `${q.id}: you chose ${h.choice}, correct is ${q.answer}`;
});
}
const bank = [
{ id: "q1", text: "Which method adds to the end of an array?",
choices: ["push", "pop", "shift", "unshift"], answer: "push" },
{ id: "q2", text: "typeof null is...", choices: ["null", "object", "undefined"],
answer: "object" },
{ id: "q3", text: "Which compares without type coercion?",
choices: ["==", "===", "="], answer: "===" },
];
const random = seededRandom(42);
const prepared = bank.map((q) => prepareQuestion(q, random));
prepared.forEach((q) => console.log(`${q.id}: ${q.choices.join(" | ")}`));
// The learner clicks by visible text, so shuffling cannot break scoring.
const clicks = ["push", "null", "==="];
const history = prepared.map((q, i) => ({
id: q.id,
choice: clicks[i],
correct: clicks[i] === q.answer,
}));
const score = history.filter((h) => h.correct).length;
console.log(`Score: ${score}/${bank.length}`);
review(history, bank).forEach((line) => console.log("Review " + line));
console.log("Original q1 order kept:", bank[0].choices.join(","));Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Quiz ကို index၊ score၊ answered၊ history ပါတဲ့ state machine သေးသေးလေးအဖြစ် မြင်ပါ
- submit() ထဲက answered guard က double click နဲ့ score နှစ်ခါတိုးတာကို တားပါတယ်
- အဖြေကို index မဟုတ်ဘဲ value နဲ့ သိမ်းရင် shuffle လုပ်လည်း အဖြေစစ်တာ မှန်နေပါတယ်
- Percent ကို number နဲ့ ဆုံးဖြတ်ပြီး toFixed() ကို ပြခါနီးမှ သုံးပါ
- UI က textContent နဲ့ state ကို ဆွဲရုံပဲ၊ စာမေးပွဲအစစ်ဆိုရင် server ပေါ်မှာ စစ်ပါ
MDN — JavaScript Guide — MDN Web Docs