|
| 1 | +const questions = [ |
| 2 | + { |
| 3 | + question: "What is (2 + 2)/2*2?", |
| 4 | + options: ["3", "4", "5", "6"], |
| 5 | + answer: "4", |
| 6 | + }, |
| 7 | + { |
| 8 | + question: "Which planet is known as the Red Planet?", |
| 9 | + options: ["Earth", "Mars", "Jupiter", "Saturn"], |
| 10 | + answer: "Mars", |
| 11 | + }, |
| 12 | + { |
| 13 | + question: "How many days do we have in a week?", |
| 14 | + options: ["1", "4", "7", "10"], |
| 15 | + answer: "7", |
| 16 | + }, |
| 17 | + { |
| 18 | + question: "How many letters are there in the English alphabet?", |
| 19 | + options: ["20", "26", "100", "0"], |
| 20 | + answer: "26", |
| 21 | + }, |
| 22 | + { |
| 23 | + question: "Which month of the year has the least number of days?", |
| 24 | + options: ["January", "March", "February", "December"], |
| 25 | + answer: "February", |
| 26 | + }, |
| 27 | + { |
| 28 | + question: "Which animal is called King of Jungle?", |
| 29 | + options: ["Lion", "Mars", "Dog", "Elephant"], |
| 30 | + answer: "Lion", |
| 31 | + }, |
| 32 | + { |
| 33 | + question: "Which is the tallest animal on the earth?", |
| 34 | + options: ["Giraffe", "Lion", "Monkey", "My Dad"], |
| 35 | + answer: "Giraffe", |
| 36 | + }, |
| 37 | + { |
| 38 | + question: "Which festival is known as the festival of colors?", |
| 39 | + options: ["Diwali", "Christmas", "Holi", "New year"], |
| 40 | + answer: "Holi", |
| 41 | + }, |
| 42 | + { |
| 43 | + question: "What is the top color in a rainbow?", |
| 44 | + options: ["Orange", "Pink", "Blue", "Red"], |
| 45 | + answer: "Red", |
| 46 | + }, |
| 47 | + { |
| 48 | + question: "How many zeros are there in one hundred thousand?", |
| 49 | + options: ["ten", "one", "seven", "five"], |
| 50 | + answer: "five", |
| 51 | + }, |
| 52 | +]; |
| 53 | + |
| 54 | +let currentQuestionIndex = 0; |
| 55 | +let score = 0; |
| 56 | + |
| 57 | +const questionText = document.getElementById("question-text"); |
| 58 | +const optionsList = document.getElementById("options-list"); |
| 59 | +const nextButton = document.getElementById("next-button"); |
| 60 | +const resultText = document.getElementById("result-text"); |
| 61 | + |
| 62 | +function showQuestion() { |
| 63 | + const currentQuestion = questions[currentQuestionIndex]; |
| 64 | + questionText.textContent = `Question ${currentQuestionIndex + 1}: ${ |
| 65 | + currentQuestion.question |
| 66 | + }`; |
| 67 | + |
| 68 | + optionsList.innerHTML = ""; |
| 69 | + currentQuestion.options.forEach((option, index) => { |
| 70 | + const listItem = document.createElement("li"); |
| 71 | + listItem.innerHTML = `<input type="radio" name="answer" value="${option}"> Option ${String.fromCharCode( |
| 72 | + 65 + index |
| 73 | + )}: ${option}`; |
| 74 | + optionsList.appendChild(listItem); |
| 75 | + }); |
| 76 | + |
| 77 | + nextButton.disabled = true; |
| 78 | +} |
| 79 | + |
| 80 | +function checkAnswer() { |
| 81 | + const selectedAnswer = document.querySelector('input[name="answer"]:checked'); |
| 82 | + |
| 83 | + if (!selectedAnswer) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + const currentQuestion = questions[currentQuestionIndex]; |
| 88 | + if (selectedAnswer.value === currentQuestion.answer) { |
| 89 | + score++; |
| 90 | + } |
| 91 | + |
| 92 | + currentQuestionIndex++; |
| 93 | + |
| 94 | + if (currentQuestionIndex < questions.length) { |
| 95 | + showQuestion(); |
| 96 | + } else { |
| 97 | + showResult(); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +function showResult() { |
| 102 | + questionText.textContent = "Quiz Completed!"; |
| 103 | + optionsList.innerHTML = ""; |
| 104 | + nextButton.style.display = "none"; |
| 105 | + resultText.textContent = `Your Score: ${score} out of ${questions.length}`; |
| 106 | +} |
| 107 | + |
| 108 | +nextButton.addEventListener("click", checkAnswer); |
| 109 | +optionsList.addEventListener("change", () => { |
| 110 | + nextButton.disabled = false; |
| 111 | +}); |
| 112 | + |
| 113 | +showQuestion(); |
0 commit comments