Spaces:
Sleeping
Sleeping
| let currentId = 100; // μμ λ¬Έμ λ²νΈ (μν JSONμ΄ 100λ²λΆν°λΌκ³ κ°μ ) | |
| async function loadQuestion(id) { | |
| const res = await fetch(`/question/${id}`); | |
| const q = await res.json(); | |
| if (q.error) { | |
| document.getElementById("question-area").innerHTML = `<p>λ¬Έμ λ₯Ό μ°Ύμ μ μμ΅λλ€.</p>`; | |
| return; | |
| } | |
| currentId = q.id; | |
| let html = `<h2>${q.id}. ${q.question}</h2>`; | |
| for (const [key, val] of Object.entries(q.options)) { | |
| html += `<label><input type="radio" name="answer" value="${key}"> ${key}. ${val}</label><br>`; | |
| } | |
| html += `<button onclick="submitAnswer(${q.id})">μ μΆ</button>`; | |
| document.getElementById("question-area").innerHTML = html; | |
| document.getElementById("result").innerHTML = ""; | |
| document.getElementById("next-btn").style.display = "none"; | |
| } | |
| async function submitAnswer(id) { | |
| const selected = document.querySelector("input[name='answer']:checked"); | |
| if (!selected) { | |
| alert("λ΅μ μ ννμΈμ!"); | |
| return; | |
| } | |
| const res = await fetch("/check_answer", { | |
| method: "POST", | |
| headers: {"Content-Type": "application/json"}, | |
| body: JSON.stringify({id: id, answer: selected.value}) | |
| }); | |
| const result = await res.json(); | |
| let msg = result.correct ? "β μ λ΅μ λλ€!" : "β μ€λ΅μ λλ€!"; | |
| msg += `<br>μ λ΅: ${result.correct_answer}`; | |
| msg += `<br>ν΄μ€: ${result.explanation}`; | |
| document.getElementById("result").innerHTML = msg; | |
| // "λ€μ λ¬Έμ " λ²νΌ νμ | |
| document.getElementById("next-btn").style.display = "inline-block"; | |
| } | |
| function nextQuestion() { | |
| loadQuestion(currentId + 1); | |
| } | |
| // μμ μ 첫 λ¬Έμ λ‘λ | |
| window.onload = () => { | |
| loadQuestion(currentId); | |
| }; | |