Changed around line 1
+ const questions = [
+ {
+ question: "What is the capital of France?",
+ options: ["London", "Berlin", "Paris", "Madrid"],
+ correct: 2
+ },
+ {
+ question: "Which planet is closest to the Sun?",
+ options: ["Venus", "Mercury", "Mars", "Earth"],
+ correct: 1
+ },
+ {
+ question: "What is 2 + 2?",
+ options: ["3", "4", "5", "6"],
+ correct: 1
+ }
+ ];
+
+ let currentQuestion = 0;
+ let score = 0;
+
+ const startBtn = document.getElementById('start-btn');
+ const retryBtn = document.getElementById('retry-btn');
+ const quizStart = document.getElementById('quiz-start');
+ const quizQuestions = document.getElementById('quiz-questions');
+ const quizResults = document.getElementById('quiz-results');
+ const questionEl = document.getElementById('question');
+ const optionsContainer = document.querySelector('.options-container');
+ const progressBar = document.getElementById('progress');
+ const scoreEl = document.getElementById('score');
+
+ function startQuiz() {
+ quizStart.classList.remove('active');
+ quizStart.classList.add('hidden');
+ quizQuestions.classList.remove('hidden');
+ quizQuestions.classList.add('active');
+ showQuestion();
+ }
+
+ function showQuestion() {
+ const question = questions[currentQuestion];
+ questionEl.textContent = question.question;
+ optionsContainer.innerHTML = '';
+
+ question.options.forEach((option, index) => {
+ const button = document.createElement('button');
+ button.classList.add('option');
+ button.textContent = option;
+ button.addEventListener('click', () => selectOption(index));
+ optionsContainer.appendChild(button);
+ });
+
+ updateProgress();
+ }
+
+ function selectOption(index) {
+ const options = document.querySelectorAll('.option');
+ options.forEach(option => option.classList.remove('selected'));
+ options[index].classList.add('selected');
+
+ if (index === questions[currentQuestion].correct) {
+ score++;
+ }
+
+ setTimeout(() => {
+ currentQuestion++;
+
+ if (currentQuestion < questions.length) {
+ showQuestion();
+ } else {
+ showResults();
+ }
+ }, 500);
+ }
+
+ function updateProgress() {
+ const progress = (currentQuestion / questions.length) * 100;
+ progressBar.style.width = `${progress}%`;
+ }
+
+ function showResults() {
+ quizQuestions.classList.remove('active');
+ quizQuestions.classList.add('hidden');
+ quizResults.classList.remove('hidden');
+ quizResults.classList.add('active');
+ scoreEl.textContent = `${score}/${questions.length}`;
+
+ // Save results to MongoDB (would require backend integration)
+ saveResults(score);
+ }
+
+ function resetQuiz() {
+ currentQuestion = 0;
+ score = 0;
+ quizResults.classList.remove('active');
+ quizResults.classList.add('hidden');
+ quizStart.classList.remove('hidden');
+ quizStart.classList.add('active');
+ }
+
+ function saveResults(score) {
+ // This function would typically make an API call to your backend
+ // to save the score in MongoDB
+ console.log('Saving score:', score);
+ }
+
+ startBtn.addEventListener('click', startQuiz);
+ retryBtn.addEventListener('click', resetQuiz);