|
| 1 | +const questions = [ |
| 2 | + { |
| 3 | + question: 'What is the capital of France?', |
| 4 | + answers: ['Berlin', 'Madrid', 'Paris', 'Rome'], |
| 5 | + correct: 2, |
| 6 | + }, |
| 7 | + { |
| 8 | + question: 'Which planet is known as the Red Planet?', |
| 9 | + answers: ['Earth', 'Mars', 'Jupiter', 'Saturn'], |
| 10 | + correct: 1, |
| 11 | + }, |
| 12 | + { |
| 13 | + question: 'What is the largest ocean on Earth?', |
| 14 | + answers: ['Atlantic', 'Indian', 'Arctic', 'Pacific'], |
| 15 | + correct: 3, |
| 16 | + }, |
| 17 | + { |
| 18 | + question: "Who wrote 'Romeo and Juliet'?", |
| 19 | + answers: ['Shakespeare', 'Dickens', 'Hemingway', 'Austen'], |
| 20 | + correct: 0, |
| 21 | + }, |
| 22 | +]; |
| 23 | + |
| 24 | +let currentQuestionIndex = 0; |
| 25 | +let score = 0; |
| 26 | + |
| 27 | +const questionElement = document.getElementById('question'); |
| 28 | +const answerButtons = document.querySelectorAll('.answer-btn'); |
| 29 | +const nextButton = document.getElementById('next-btn'); |
| 30 | +const resultContainer = document.getElementById('result-container'); |
| 31 | +const restartButton = document.getElementById('restart-btn'); |
| 32 | +const scoreElement = document.getElementById('score'); |
| 33 | + |
| 34 | +// Load question and answers |
| 35 | +const loadQuestion = () => { |
| 36 | + const currentQuestion = questions[currentQuestionIndex]; |
| 37 | + questionElement.textContent = currentQuestion.question; |
| 38 | + |
| 39 | + answerButtons.forEach((button, index) => { |
| 40 | + button.textContent = currentQuestion.answers[index]; |
| 41 | + button.disabled = false; |
| 42 | + button.style.backgroundColor = '#4CAF50'; // Reset button color |
| 43 | + }); |
| 44 | + |
| 45 | + nextButton.disabled = true; // Disable next button until an answer is selected |
| 46 | +}; |
| 47 | + |
| 48 | +// Handle answer selection |
| 49 | +const handleAnswerClick = (index) => { |
| 50 | + const currentQuestion = questions[currentQuestionIndex]; |
| 51 | + |
| 52 | + if (index === currentQuestion.correct) { |
| 53 | + score += 1; |
| 54 | + } |
| 55 | + |
| 56 | + // Disable all buttons after an answer is selected |
| 57 | + answerButtons.forEach((button, i) => { |
| 58 | + button.disabled = true; |
| 59 | + if (i === currentQuestion.correct) { |
| 60 | + button.style.backgroundColor = '#4CAF50'; // Correct answer |
| 61 | + } else { |
| 62 | + button.style.backgroundColor = '#f44336'; // Incorrect answers |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + nextButton.disabled = false; // Enable next button |
| 67 | +}; |
| 68 | + |
| 69 | +// Show quiz result |
| 70 | +const showResult = () => { |
| 71 | + document.getElementById('quiz-box').classList.add('hidden'); |
| 72 | + resultContainer.classList.remove('hidden'); |
| 73 | + scoreElement.textContent = `You scored ${score} out of ${questions.length}`; |
| 74 | +}; |
| 75 | + |
| 76 | +// Move to next question |
| 77 | +const nextQuestion = () => { |
| 78 | + currentQuestionIndex += 1; |
| 79 | + if (currentQuestionIndex < questions.length) { |
| 80 | + loadQuestion(); |
| 81 | + } else { |
| 82 | + showResult(); |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +// Restart the quiz |
| 87 | +const restartQuiz = () => { |
| 88 | + currentQuestionIndex = 0; |
| 89 | + score = 0; |
| 90 | + resultContainer.classList.add('hidden'); |
| 91 | + document.getElementById('quiz-box').classList.remove('hidden'); |
| 92 | + loadQuestion(); |
| 93 | +}; |
| 94 | + |
| 95 | +// Add event listeners to answer buttons |
| 96 | +answerButtons.forEach((button, index) => { |
| 97 | + button.addEventListener('click', () => handleAnswerClick(index)); |
| 98 | +}); |
| 99 | + |
| 100 | +// Add event listener to next button |
| 101 | +nextButton.addEventListener('click', nextQuestion); |
| 102 | + |
| 103 | +// Add event listener to restart button |
| 104 | +restartButton.addEventListener('click', restartQuiz); |
| 105 | + |
| 106 | +// Start the quiz |
| 107 | +loadQuestion(); |
0 commit comments