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);
};