Spaces:
Running
Running
File size: 1,694 Bytes
8f0fa74 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
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);
};
|