Changed around line 1
+ document.addEventListener('DOMContentLoaded', () => {
+ const quizData = [
+ {
+ question: "What is the capital of France?",
+ options: ["London", "Berlin", "Paris", "Madrid"],
+ correct: 2
+ },
+ {
+ question: "Which planet is known as the Red Planet?",
+ options: ["Venus", "Mars", "Jupiter", "Saturn"],
+ correct: 1
+ },
+ {
+ question: "What is 2 + 2?",
+ options: ["3", "4", "5", "6"],
+ correct: 1
+ }
+ ];
+
+ let currentQuestion = 0;
+ let score = 0;
+ let quizStarted = false;
+
+ const questionEl = document.getElementById('question');
+ const optionsEl = document.getElementById('options');
+ const startBtn = document.getElementById('start-btn');
+ const nextBtn = document.getElementById('next-btn');
+ const progressFill = document.querySelector('.progress-fill');
+ const resultsSection = document.getElementById('results');
+ const scoreEl = document.getElementById('score');
+ const retryBtn = document.getElementById('retry-btn');
+
+ function startQuiz() {
+ quizStarted = true;
+ currentQuestion = 0;
+ score = 0;
+ startBtn.style.display = 'none';
+ nextBtn.style.display = 'block';
+ resultsSection.style.display = 'none';
+ showQuestion();
+ }
+
+ function showQuestion() {
+ const question = quizData[currentQuestion];
+ questionEl.textContent = question.question;
+
+ optionsEl.innerHTML = '';
+ question.options.forEach((option, index) => {
+ const button = document.createElement('button');
+ button.className = 'option-btn';
+ button.textContent = option;
+ button.addEventListener('click', () => selectOption(index));
+ optionsEl.appendChild(button);
+ });
+
+ updateProgress();
+ }
+
+ function selectOption(index) {
+ const buttons = optionsEl.getElementsByClassName('option-btn');
+ Array.from(buttons).forEach(button => {
+ button.disabled = true;
+ button.style.cursor = 'default';
+ });
+
+ if (index === quizData[currentQuestion].correct) {
+ buttons[index].style.background = '#22c55e';
+ buttons[index].style.color = 'white';
+ score++;
+ } else {
+ buttons[index].style.background = '#ef4444';
+ buttons[index].style.color = 'white';
+ buttons[quizData[currentQuestion].correct].style.background = '#22c55e';
+ buttons[quizData[currentQuestion].correct].style.color = 'white';
+ }
+ }
+
+ function nextQuestion() {
+ currentQuestion++;
+ if (currentQuestion < quizData.length) {
+ showQuestion();
+ } else {
+ showResults();
+ }
+ }
+
+ function updateProgress() {
+ const progress = ((currentQuestion + 1) / quizData.length) * 100;
+ progressFill.style.width = `${progress}%`;
+ }
+
+ function showResults() {
+ const quizSection = document.getElementById('quiz-container');
+ quizSection.style.display = 'none';
+ resultsSection.style.display = 'block';
+
+ const percentage = Math.round((score / quizData.length) * 100);
+ scoreEl.textContent = percentage;
+
+ const messageEl = document.getElementById('score-message');
+ if (percentage >= 80) {
+ messageEl.textContent = "Excellent work!";
+ } else if (percentage >= 60) {
+ messageEl.textContent = "Good job!";
+ } else {
+ messageEl.textContent = "Keep practicing!";
+ }
+ }
+
+ startBtn.addEventListener('click', startQuiz);
+ nextBtn.addEventListener('click', nextQuestion);
+ retryBtn.addEventListener('click', () => {
+ document.getElementById('quiz-container').style.display = 'block';
+ startQuiz();
+ });
+ });