68 lines
1.8 KiB
JavaScript
68 lines
1.8 KiB
JavaScript
// Логика викторины. Данные вопросов — в questions.js (массив QUESTIONS).
|
|
let idx = 0;
|
|
let score = 0;
|
|
|
|
const scrStart = document.getElementById("screen-start");
|
|
const scrQuiz = document.getElementById("screen-quiz");
|
|
const scrResult = document.getElementById("screen-result");
|
|
|
|
document.getElementById("btn-start").addEventListener("click", start);
|
|
document.getElementById("btn-again").addEventListener("click", start);
|
|
|
|
function start() {
|
|
idx = 0;
|
|
score = 0;
|
|
show(scrQuiz);
|
|
renderQ();
|
|
}
|
|
|
|
function renderQ() {
|
|
const q = QUESTIONS[idx];
|
|
document.getElementById("progress").textContent =
|
|
"Вопрос " + (idx + 1) + " из " + QUESTIONS.length;
|
|
document.getElementById("q-text").textContent = q.text;
|
|
|
|
const box = document.getElementById("q-options");
|
|
box.innerHTML = "";
|
|
q.options.forEach(function (opt, i) {
|
|
const b = document.createElement("button");
|
|
b.textContent = opt;
|
|
b.addEventListener("click", function () {
|
|
chkAns(i);
|
|
});
|
|
box.appendChild(b);
|
|
});
|
|
}
|
|
|
|
function chkAns(i) {
|
|
if (i === QUESTIONS[idx].correct) {
|
|
score++;
|
|
}
|
|
idx++;
|
|
if (idx < QUESTIONS.length) {
|
|
renderQ();
|
|
} else {
|
|
showResult();
|
|
}
|
|
}
|
|
|
|
function showResult() {
|
|
show(scrResult);
|
|
document.getElementById("result-text").textContent =
|
|
"Правильных ответов: " + score + " из " + QUESTIONS.length + ". " + verdict();
|
|
}
|
|
|
|
function verdict() {
|
|
if (score === QUESTIONS.length) return "Идеально!";
|
|
if (score >= 7) return "Отличный результат.";
|
|
if (score >= 4) return "Неплохо, но есть куда расти.";
|
|
return "Стоит попробовать ещё раз.";
|
|
}
|
|
|
|
function show(screen) {
|
|
scrStart.classList.add("hidden");
|
|
scrQuiz.classList.add("hidden");
|
|
scrResult.classList.add("hidden");
|
|
screen.classList.remove("hidden");
|
|
}
|