sfnc ggg dfdd fff gggg
const questions = [ { question: 'What is the capital of France?', answerArticleLink: 'URL_TO_ANSWER_ARTICLE', answers: [ { text: 'Berlin', correct: false }, { text: 'Paris', correct: true }, { text: 'Madrid', correct: false }, { text: 'Rome', correct: false } ] }, // Add more questions as needed ]; const quizContainer = document.getElementById('quiz-container'); const questionContainer = document.getElementById('question-container'); const resultContainer = document.getElementById('result-container'); const submitButton = document.getElementById('submit-button'); let currentQuestionIndex = 0; function startQuiz() { renderQuestion(currentQuestionIndex); submitButton.addEventListener('click', checkAnswer); } function renderQuestion(index) { if (index >= questions.length) { showResults(); return; } const question = questions[index]; const questionDiv = document.createElement('div'); questionDiv.innerHTML = `${question.question}
${question.answers.map(renderAnswer).join('')} `; questionContainer.innerHTML = ''; questionContainer.appendChild(questionDiv); } function renderAnswer(answer) { return ` `; } function checkAnswer() { const selectedAnswer = document.querySelector('input[name="answer"]:checked'); if (selectedAnswer) { const selectedQuestion = selectedAnswer.closest('div'); const index = Array.from(questionContainer.children).indexOf(selectedQuestion); if (questions[index].answers.find(a => a.text === selectedAnswer.value && a.correct)) { selectedQuestion.classList.add('correct'); } else { selectedQuestion.classList.add('incorrect'); showAnswerArticle(questions[index].answerArticleLink); return; } currentQuestionIndex++; renderQuestion(currentQuestionIndex); } } function showAnswerArticle(articleLink) { resultContainer.innerHTML = `Oops! That's incorrect. Check out the answer in this article: Answer Article
`; } function showResults() { const score = document.querySelectorAll('.correct').length; resultContainer.innerHTML = `You scored ${score} out of ${questions.length}.`; } startQuiz();
0 Comments