import { useState, useEffect, useRef } from "react"; const QUESTIONS = [ { q: "What is the plural of 'child'?", o: ["A. childs", "B. childes", "C. children", "D. childrens"], a: "C" }, { q: "Choose the correct sentence:", o: ["A. She don't like cats", "B. She doesn't likes cats", "C. She doesn't like cats", "D. She not like cats"], a: "C" }, { q: "What is the past tense of 'go'?", o: ["A. goed", "B. went", "C. gone", "D. goes"], a: "B" }, { q: "Which word is an adjective?", o: ["A. quickly", "B. beauty", "C. beautiful", "D. beautify"], a: "C" }, { q: "What does 'enormous' mean?", o: ["A. tiny", "B. very large", "C. colorful", "D. ancient"], a: "B" }, { q: "Choose the correct question tag: 'She is smart, ___?'", o: ["A. is she", "B. isn't she", "C. doesn't she", "D. wasn't she"], a: "B" }, { q: "Which sentence uses the Present Perfect correctly?", o: ["A. I have went there", "B. I have go there", "C. I have been there", "D. I been there"], a: "C" }, { q: "What is the synonym of 'happy'?", o: ["A. sad", "B. joyful", "C. angry", "D. tired"], a: "B" }, { q: "Which word is spelled correctly?", o: ["A. recieve", "B. beleive", "C. achieve", "D. freind"], a: "C" }, { q: "Choose the correct preposition: 'She arrived ___ Monday.'", o: ["A. in", "B. at", "C. on", "D. by"], a: "C" }, ]; const STORAGE_KEY = "quiz_results_v1"; export default function QuizApp() { const [phase, setPhase] = useState("name"); // name | quiz | done const [name, setName] = useState(""); const [answers, setAnswers] = useState({}); const [startTime, setStartTime] = useState(null); const [elapsed, setElapsed] = useState(0); const [submitted, setSubmitted] = useState(false); const timerRef = useRef(null); useEffect(() => { if (phase === "quiz") { const t = Date.now(); setStartTime(t); timerRef.current = setInterval(() => setElapsed(Date.now() - t), 1000); } return () => clearInterval(timerRef.current); }, [phase]); const fmt = (ms) => { const s = Math.floor(ms / 1000); const m = Math.floor(s / 60); return `${String(m).padStart(2,"0")}:${String(s % 60).padStart(2,"0")}`; }; const answered = Object.keys(answers).length; const pct = Math.round((answered / QUESTIONS.length) * 100); const handleStart = () => { if (name.trim().length < 2) return; setPhase("quiz"); }; const handleAnswer = (i, opt) => { if (submitted) return; setAnswers(prev => ({ ...prev, [i]: opt })); }; const handleSubmit = async () => { if (answered < QUESTIONS.length) return; clearInterval(timerRef.current); const duration = Date.now() - startTime; let score = 0; const detail = QUESTIONS.map((q, i) => { const correct = answers[i] === q.a; if (correct) score++; return { question: q.q, chosen: answers[i], correct: q.a, isCorrect: correct }; }); const result = { name: name.trim(), score, total: QUESTIONS.length, duration, submittedAt: new Date().toISOString(), detail, }; try { const existing = await window.storage.get(STORAGE_KEY, true).catch(() => null); const list = existing ? JSON.parse(existing.value) : []; list.push(result); await window.storage.set(STORAGE_KEY, JSON.stringify(list), true); } catch (e) {} setSubmitted(true); setPhase("done"); }; if (phase === "name") return (
๐Ÿ“

Olimpiade Bahasa Inggris

10 Soal ยท Waktu dicatat otomatis

setName(e.target.value)} onKeyDown={e => e.key === "Enter" && handleStart()} placeholder="Tulis nama kamu di sini..." style={{width:"100%",border:"2px solid #e5e7eb",borderRadius:10,padding:"10px 14px",fontSize:14,outline:"none",marginBottom:16}} />

Jawaban kamu akan tersimpan otomatis saat selesai

); if (phase === "done") return (
{Object.values(answers).filter((a,i)=>a===QUESTIONS[i].a).length>=8?"๐Ÿ†":Object.values(answers).filter((a,i)=>a===QUESTIONS[i].a).length>=6?"๐Ÿ‘":"๐Ÿ’ช"}

Selesai, {name}!

{QUESTIONS.filter((_,i)=>answers[i]===QUESTIONS[i].a).length}/{QUESTIONS.length}
Waktu: {fmt(elapsed)}

Hasilmu sudah tersimpan. Terima kasih sudah mengerjakan! ๐ŸŽ‰

); return (
{/* Header */}
๐Ÿ“ Olimpiade B. Inggris
{name}
{fmt(elapsed)}
{answered}/{QUESTIONS.length} dijawab
{/* Questions */}
{QUESTIONS.map((q, i) => (
Soal {i+1}
{q.q}
{q.o.map(opt => ( ))}
))}
{/* Submit */}
{answered < QUESTIONS.length &&

โš ๏ธ Masih ada {QUESTIONS.length-answered} soal yang belum dijawab

}
); }