document.addEventListener('DOMContentLoaded', async () => { const app = document.getElementById('app'); const resp = await fetch('/api/generate'); const data = await resp.json(); if (data.error) { app.innerHTML = `

Ошибка получения теста: ${data.error}

`; return; } if (!Array.isArray(data) || data.length === 0) { app.innerHTML = `

Нет данных

`; return; } const questions = data; let current = 0; const answers = new Array(questions.length).fill(null); function renderQuestion(i) { const q = questions[i]; const opts = q.options || []; const html = `

Вопрос ${i+1}: ${q.question}

${opts.map((opt, idx) => ``).join('')}
`; app.innerHTML = html; document.querySelectorAll('.options button').forEach(btn => { btn.onclick = () => { answers[i] = parseInt(btn.dataset.idx,10); if (i+1 < questions.length) renderQuestion(i+1); else showResult(); }; }); } function showResult() { let correct = 0; questions.forEach((q, idx) => { if (answers[idx] === q.correct) correct++; }); const percent = Math.round((correct / questions.length) * 100); const verdict = percent >= 80 ? 'Прошел' : 'Не прошел'; app.innerHTML = `

Результат: ${percent}% – ${verdict}

`; } renderQuestion(current); });