Spaces:
Sleeping
Sleeping
| let currentId = 0; | |
| let chosen = null; | |
| function loadQuestion(qid = null) { | |
| let url = "/api/question"; | |
| if (qid) url += "?id=" + qid; | |
| $.getJSON(url, function(data) { | |
| currentId = data.id; | |
| $("#qno").text("๋ฌธ์ " + data.qno); | |
| $("#stem").text(data.stem); | |
| $("#options").empty(); | |
| data.options.forEach(opt => { | |
| let btn = $("<button>") | |
| .addClass("list-group-item list-group-item-action") | |
| .text(opt) | |
| .click(function() { | |
| $("#options button").removeClass("active"); | |
| $(this).addClass("active"); | |
| chosen = opt; | |
| }); | |
| $("#options").append(btn); | |
| }); | |
| $("#result").empty(); | |
| }); | |
| } | |
| function submitAnswer() { | |
| if (!chosen) { | |
| alert("์ ํ์ง๋ฅผ ๊ณ ๋ฅด์ธ์!"); | |
| return; | |
| } | |
| $.ajax({ | |
| url: "/api/answer", | |
| type: "POST", | |
| contentType: "application/json", | |
| data: JSON.stringify({ question_id: currentId, chosen: chosen }), | |
| success: function(resp) { | |
| let resultText = resp.correct | |
| ? `<div class="alert alert-success">์ ๋ต์ ๋๋ค! โ </div>` | |
| : `<div class="alert alert-danger">ํ๋ ธ์ต๋๋ค โ<br>์ ๋ต: ${resp.answer}</div>`; | |
| $("#result").html(resultText); | |
| } | |
| }); | |
| } | |
| function loadNext() { | |
| $.getJSON("/api/next?current_id=" + currentId, function(data) { | |
| if (data.end) { | |
| $("#qno").text("๋!"); | |
| $("#stem").text(data.message); | |
| $("#options").empty(); | |
| $("#result").empty(); | |
| } else { | |
| currentId = data.id; | |
| $("#qno").text("๋ฌธ์ " + data.qno); | |
| $("#stem").text(data.stem); | |
| $("#options").empty(); | |
| chosen = null; | |
| data.options.forEach(opt => { | |
| let btn = $("<button>") | |
| .addClass("list-group-item list-group-item-action") | |
| .text(opt) | |
| .click(function() { | |
| $("#options button").removeClass("active"); | |
| $(this).addClass("active"); | |
| chosen = opt; | |
| }); | |
| $("#options").append(btn); | |
| }); | |
| $("#result").empty(); | |
| } | |
| }); | |
| } | |
| $(document).ready(function() { | |
| loadQuestion(); | |
| $("#submitBtn").click(submitAnswer); | |
| $("#nextBtn").click(loadNext); | |
| }); | |