Вопросы и логика: 10 вопросов, подсчёт очков, экран результата
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
// Логика викторины. Данные вопросов — в 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");
|
||||
}
|
||||
Reference in New Issue
Block a user