Trivia Quiz body { font-family: 'Inter', sans-serif; } .quiz-container { max-width: 600px; margin: 2rem auto; padding: 2rem; background-color: #ffffff; border-radius: 1rem; box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05); } .option-btn { transition: background-color 0.3s, transform 0.2s; } .option-btn:hover { transform: translateY(-2px); background-color: #f0f4f8; } .correct { background-color: #d1fae5 !important; /* Tailwind green-100 */ border-color: #6ee7b7 !important; /* Tailwind green-300 */ } .incorrect { background-color: #fee2e2 !important; /* Tailwind red-100 */ border-color: #fca5a5 !important; /* Tailwind red-300 */ } General Knowledge Quiz Next Question Quiz Complete! Restart Quiz const questions = [ { question: "What is the capital of Japan?", options: ["Beijing", "Seoul", "Tokyo", "Bangkok"], answer: "Tokyo" }, { question: "Which planet is known as the Red Planet?", options: ["Earth", "Mars", "Jupiter", "Venus"], answer: "Mars" }, { question: "Who wrote the play 'Romeo and Juliet'?", options: ["Charles Dickens", "William Shakespeare", "Jane Austen", "Mark Twain"], answer: "William Shakespeare" }, { question: "What is the largest mammal in the world?", options: ["Elephant", "Blue Whale", "Giraffe", "Great White Shark"], answer: "Blue Whale" }, { question: "In which year did the Titanic sink?", options: ["1905", "1912", "1918", "1923"], answer: "1912" } ]; let currentQuestionIndex = 0; let score = 0; const questionTextEl = document.getElementById('question-text'); const optionsContainerEl = document.getElementById('options-container'); const feedbackTextEl = document.getElementById('feedback-text'); const nextBtn = document.getElementById('next-btn'); const scoreAreaEl = document.getElementById('score-area'); const scoreTextEl = document.getElementById('score-text'); const questionAreaEl = document.getElementById('question-area'); const restartBtn = document.getElementById('restart-btn'); function startQuiz() { currentQuestionIndex = 0; score = 0; scoreAreaEl.classList.add('hidden'); questionAreaEl.classList.remove('hidden'); feedbackTextEl.innerHTML = ' '; nextBtn.classList.add('hidden'); showQuestion(); } function showQuestion() { // Reset state feedbackTextEl.innerHTML = ' '; nextBtn.classList.add('hidden'); optionsContainerEl.innerHTML = ''; const currentQuestion = questions[currentQuestionIndex]; questionTextEl.textContent = currentQuestion.question; currentQuestion.options.forEach(option => { const button = document.createElement('button'); button.textContent = option; button.classList.add('option-btn', 'w-full', 'text-left', 'p-4', 'border-2', 'border-gray-300', 'rounded-lg', 'hover:bg-gray-100'); button.onclick = () => selectAnswer(button, option, currentQuestion.answer); optionsContainerEl.appendChild(button); }); } function selectAnswer(button, selectedOption, correctAnswer) { const allOptions = optionsContainerEl.getElementsByTagName('button'); for (let btn of allOptions) { btn.disabled = true; // Disable all options after one is selected if (btn.textContent === correctAnswer) { btn.classList.add('correct'); } else { btn.classList.add('incorrect'); } } if (selectedOption === correctAnswer) { feedbackTextEl.textContent = "Correct!"; feedbackTextEl.classList.remove('text-red-500'); feedbackTextEl.classList.add('text-green-500'); score++; } else { feedbackTextEl.textContent = `Wrong! The correct answer was ${correctAnswer}.`; feedbackTextEl.classList.remove('text-green-500'); feedbackTextEl.classList.add('text-red-500'); } nextBtn.classList.remove('hidden'); } function showNextQuestion() { currentQuestionIndex++; if (currentQuestionIndex < questions.length) { showQuestion(); } else { showScore(); } } function showScore() { questionAreaEl.classList.add('hidden'); feedbackTextEl.innerHTML = ' '; nextBtn.classList.add('hidden'); scoreAreaEl.classList.remove('hidden'); scoreTextEl.textContent = `You scored ${score} out of ${questions.length}!`; } nextBtn.addEventListener('click', showNextQuestion); restartBtn.addEventListener('click', startQuiz); // Initial start startQuiz();