// quiz — judge the tick boxes of one quiz, explain what went wrong // // Every label the reader sees is written into the markup by quiz.lua, so the // wording follows the book's language and nothing is translated twice. A // quiz carries: // data-quiz-score "{n} of {m} questions correct" ({n}/{m} substituted) // data-quiz-right verdict of a question answered correctly // data-quiz-wrong verdict of a question answered wrongly // // A question is right only when every one of its options is in the right // state: ticked when true, left empty when false. That is the "multiple // true/false" rule of the exam, and it is why a half-ticked question counts // as wrong. (function () { "use strict"; function options(quiz) { return quiz.querySelectorAll(".quiz-opt"); } function judge(quiz) { var questions = quiz.querySelectorAll(".quiz-q"); var right = 0; questions.forEach(function (q) { var ok = true; q.querySelectorAll(".quiz-opt").forEach(function (opt) { var box = opt.querySelector(".quiz-box"); if (!box) return; var want = opt.dataset.correct === "1"; var hit = box.checked === want; opt.classList.toggle("quiz-ok", hit); opt.classList.toggle("quiz-bad", !hit); // the explanation of an option is for the reader who got it wrong var why = opt.querySelector(".quiz-why"); if (why) why.hidden = hit; if (!hit) ok = false; }); var verdict = q.querySelector(".quiz-verdict"); if (verdict) { verdict.hidden = false; verdict.textContent = ok ? (quiz.dataset.quizRight || "Correct") : (quiz.dataset.quizWrong || "Not quite"); verdict.classList.toggle("quiz-ok", ok); verdict.classList.toggle("quiz-bad", !ok); } // a question-level explanation backs up a wrong answer as a whole var qwhy = q.querySelector(".quiz-why-q"); if (qwhy) qwhy.hidden = ok; if (ok) right++; }); var score = quiz.querySelector(".quiz-score"); if (score) { var template = quiz.dataset.quizScore || "{n}/{m}"; score.innerHTML = ""; // built as nodes, not markup: the template comes from metadata var parts = template.split(/\{n\}|\{m\}/); var values = []; (template.match(/\{n\}|\{m\}/g) || []).forEach(function (token) { values.push(token === "{n}" ? right : questions.length); }); parts.forEach(function (text, i) { score.appendChild(document.createTextNode(text)); if (i < values.length) { var b = document.createElement("b"); b.textContent = String(values[i]); score.appendChild(b); } }); } } function reset(quiz) { options(quiz).forEach(function (opt) { var box = opt.querySelector(".quiz-box"); if (box) box.checked = false; opt.classList.remove("quiz-ok", "quiz-bad"); var why = opt.querySelector(".quiz-why"); if (why) why.hidden = true; }); quiz.querySelectorAll(".quiz-verdict").forEach(function (v) { v.hidden = true; v.textContent = ""; v.classList.remove("quiz-ok", "quiz-bad"); }); quiz.querySelectorAll(".quiz-why-q").forEach(function (w) { w.hidden = true; }); var score = quiz.querySelector(".quiz-score"); if (score) score.textContent = ""; } document.addEventListener("click", function (event) { var target = event.target; if (!target.closest) return; var check = target.closest(".quiz .quiz-check"); if (check) { judge(check.closest(".quiz")); return; } var again = target.closest(".quiz .quiz-reset"); if (again) { reset(again.closest(".quiz")); } }); // re-ticking a box after checking clears that row's verdict, so a stale // green or red never sits next to a box the reader has just changed document.addEventListener("change", function (event) { var box = event.target; if (!box.closest || !box.classList || !box.classList.contains("quiz-box")) return; var opt = box.closest(".quiz-opt"); if (!opt) return; opt.classList.remove("quiz-ok", "quiz-bad"); var why = opt.querySelector(".quiz-why"); if (why) why.hidden = true; }); })();