|
| 1 | +const questions = [ |
| 2 | + { |
| 3 | + question: "What is the capital of France?", |
| 4 | + answers: ["Paris", "London", "Berlin", "Madrid"] |
| 5 | + }, |
| 6 | + { |
| 7 | + question: "What is 2 + 2?", |
| 8 | + answers: ["4", "3", "5", "6"] |
| 9 | + }, |
| 10 | + { |
| 11 | + question: "What is the largest planet in our solar system?", |
| 12 | + answers: ["Jupiter", "Mars", "Earth", "Venus"] |
| 13 | + }, |
| 14 | + { |
| 15 | + question: "What is the speed of light?", |
| 16 | + answers: ["299,792,458 m/s", "150,000,000 m/s", "300,000 km/s", "299,792 km/s"] |
| 17 | + }, |
| 18 | + { |
| 19 | + question: "Who wrote 'Hamlet'?", |
| 20 | + answers: ["William Shakespeare", "Charles Dickens", "Leo Tolstoy", "Mark Twain"] |
| 21 | + }, |
| 22 | + { |
| 23 | + question: "What is the chemical symbol for water?", |
| 24 | + answers: ["H2O", "O2", "CO2", "H2"] |
| 25 | + }, |
| 26 | + { |
| 27 | + question: "What is the hardest natural substance on Earth?", |
| 28 | + answers: ["Diamond", "Gold", "Iron", "Silver"] |
| 29 | + }, |
| 30 | + { |
| 31 | + question: "Which element has the chemical symbol 'O'?", |
| 32 | + answers: ["Oxygen", "Hydrogen", "Osmium", "Oganesson"] |
| 33 | + }, |
| 34 | + { |
| 35 | + question: "Who painted the Mona Lisa?", |
| 36 | + answers: ["Leonardo da Vinci", "Pablo Picasso", "Vincent van Gogh", "Michelangelo"] |
| 37 | + }, |
| 38 | + { |
| 39 | + question: "Which country is known as the Land of the Rising Sun?", |
| 40 | + answers: ["Japan", "China", "South Korea", "Thailand"] |
| 41 | + } |
| 42 | +]; |
| 43 | + |
| 44 | +let score = 0; |
| 45 | + |
| 46 | +// Shuffle function to randomize answers |
| 47 | +function shuffle(array) { |
| 48 | + for (let i = array.length - 1; i > 0; i--) { |
| 49 | + const j = Math.floor(Math.random() * (i + 1)); |
| 50 | + [array[i], array[j]] = [array[j], array[i]]; |
| 51 | + } |
| 52 | + return array; |
| 53 | +} |
| 54 | + |
| 55 | +// Load quiz questions |
| 56 | +function loadQuiz() { |
| 57 | + const quizQuestionsDiv = document.getElementById('quiz-questions'); |
| 58 | + quizQuestionsDiv.innerHTML = ''; // Clear any previous content |
| 59 | + |
| 60 | + questions.forEach((q, index) => { |
| 61 | + const shuffledAnswers = shuffle([...q.answers]); // Randomize answers |
| 62 | + |
| 63 | + // Generate HTML for each question |
| 64 | + let questionBlock = ` |
| 65 | + <div class="question"> |
| 66 | + <h3>${q.question}</h3>`; |
| 67 | + |
| 68 | + shuffledAnswers.forEach((answer) => { |
| 69 | + questionBlock += ` |
| 70 | + <label> |
| 71 | + <input type="radio" name="question${index}" value="${answer}"> |
| 72 | + ${answer} |
| 73 | + </label>`; |
| 74 | + }); |
| 75 | + |
| 76 | + questionBlock += `</div>`; |
| 77 | + quizQuestionsDiv.innerHTML += questionBlock; |
| 78 | + }); |
| 79 | +} |
| 80 | + |
| 81 | +// Check answers and display results |
| 82 | +function checkAnswers() { |
| 83 | + const form = document.getElementById('quiz-form'); |
| 84 | + const resultDiv = document.getElementById('result'); |
| 85 | + const resultMessage = document.getElementById('result-message'); |
| 86 | + const resultScore = document.getElementById('result-score'); |
| 87 | + score = 0; |
| 88 | + |
| 89 | + // Validate answers |
| 90 | + questions.forEach((q, index) => { |
| 91 | + const userAnswer = form[`question${index}`].value; |
| 92 | + if (userAnswer === q.answers[0]) { // Check if the selected answer is correct |
| 93 | + score += 10; // Each correct answer adds 10 points |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + // Hide the quiz form and display the result |
| 98 | + form.style.display = 'none'; |
| 99 | + resultDiv.style.display = 'block'; // Show result div |
| 100 | + |
| 101 | + // Set the result message and score |
| 102 | + if (score >= 70) { |
| 103 | + resultMessage.textContent = "Congratulations, you passed!"; |
| 104 | + } else { |
| 105 | + resultMessage.textContent = "Better luck next time!"; |
| 106 | + } |
| 107 | + |
| 108 | + resultScore.textContent = `Your score is: ${score}/100`; |
| 109 | +} |
| 110 | + |
| 111 | +// Load quiz on page load |
| 112 | +document.addEventListener('DOMContentLoaded', loadQuiz); |
0 commit comments