|
| 1 | +const quizData = [ |
| 2 | + { |
| 3 | + question: "Which of the following is not a JavaScript Data Type?", |
| 4 | + a: "Undefined", |
| 5 | + b: "Number", |
| 6 | + c: "Boolean", |
| 7 | + d: "Float", |
| 8 | + correct: "d", |
| 9 | + }, |
| 10 | + { |
| 11 | + question: "Which company developed JavaScript?", |
| 12 | + a: "Netscape", |
| 13 | + b: "Bell Labs", |
| 14 | + c: "Sun Microsystems", |
| 15 | + d: "IBM", |
| 16 | + correct: "a", |
| 17 | + }, |
| 18 | + { |
| 19 | + question: "Inside which HTML element do we put the JavaScript?", |
| 20 | + a: "<script>", |
| 21 | + b: "<head>", |
| 22 | + c: "<meta>", |
| 23 | + d: "<style>", |
| 24 | + correct: "a", |
| 25 | + }, |
| 26 | + { |
| 27 | + question: "Which one is the ternary operator in JavaScript?", |
| 28 | + a: "#", |
| 29 | + b: "::", |
| 30 | + c: "&:", |
| 31 | + d: "?:", |
| 32 | + correct: "d", |
| 33 | + }, |
| 34 | + { |
| 35 | + question: "Which symbol is used for comments in JavaScript?", |
| 36 | + a: "\\", |
| 37 | + b: "//", |
| 38 | + c: "/* */", |
| 39 | + d: "*/", |
| 40 | + correct: "b", |
| 41 | + }, |
| 42 | + { |
| 43 | + question: "What are the types of pop-up boxes available in JavaScript?", |
| 44 | + a: "Alert", |
| 45 | + b: "Prompt", |
| 46 | + c: "Confirm", |
| 47 | + d: "All of the above", |
| 48 | + correct: "d", |
| 49 | + }, |
| 50 | + { |
| 51 | + question: "Which of the following methods checks if its argument is not a number?", |
| 52 | + a: "isNaN()", |
| 53 | + b: "nonNaN()", |
| 54 | + c: "NaN()", |
| 55 | + d: "None of the above", |
| 56 | + correct: "a", |
| 57 | + }, |
| 58 | +]; |
| 59 | + |
| 60 | +const quiz = document.getElementById('quiz'); |
| 61 | +const questionEl = document.getElementById('question'); |
| 62 | +const a_text = document.getElementById('a_text'); |
| 63 | +const b_text = document.getElementById('b_text'); |
| 64 | +const c_text = document.getElementById('c_text'); |
| 65 | +const d_text = document.getElementById('d_text'); |
| 66 | +const submitBtn = document.getElementById('submit'); |
| 67 | +const answerEls = document.querySelectorAll('.answer'); |
| 68 | + |
| 69 | +let currentQuiz = 0; |
| 70 | +let score = 0; |
| 71 | +let answers = []; |
| 72 | + |
| 73 | +loadQuiz(); |
| 74 | + |
| 75 | +function loadQuiz() { |
| 76 | + deselectAnswers(); |
| 77 | + const currentQuizData = quizData[currentQuiz]; |
| 78 | + questionEl.innerText = currentQuizData.question; |
| 79 | + a_text.innerText = currentQuizData.a; |
| 80 | + b_text.innerText = currentQuizData.b; |
| 81 | + c_text.innerText = currentQuizData.c; |
| 82 | + d_text.innerText = currentQuizData.d; |
| 83 | +} |
| 84 | + |
| 85 | +function getSelected() { |
| 86 | + let answer = undefined; |
| 87 | + answerEls.forEach(answerEl => { |
| 88 | + if (answerEl.checked) { |
| 89 | + answer = answerEl.id; |
| 90 | + } |
| 91 | + }); |
| 92 | + return answer; |
| 93 | +} |
| 94 | + |
| 95 | +function deselectAnswers() { |
| 96 | + answerEls.forEach(answerEl => { |
| 97 | + answerEl.checked = false; |
| 98 | + }); |
| 99 | +} |
| 100 | + |
| 101 | +submitBtn.addEventListener('click', () => { |
| 102 | + const answer = getSelected(); |
| 103 | + |
| 104 | + if (answer) { |
| 105 | + // Save the user's answer |
| 106 | + answers.push({ question: quizData[currentQuiz].question, answer, correctAnswer: quizData[currentQuiz].correct }); |
| 107 | + |
| 108 | + if (answer === quizData[currentQuiz].correct) { |
| 109 | + score++; |
| 110 | + } |
| 111 | + |
| 112 | + currentQuiz++; |
| 113 | + |
| 114 | + if (currentQuiz < quizData.length) { |
| 115 | + loadQuiz(); |
| 116 | + } else { |
| 117 | + // Show quiz result |
| 118 | + showResult(); |
| 119 | + } |
| 120 | + } |
| 121 | +}); |
| 122 | + |
| 123 | +function showResult() { |
| 124 | + quiz.innerHTML = ` |
| 125 | + <h2>You answered ${score}/${quizData.length} questions correctly</h2> |
| 126 | + <ul> |
| 127 | + ${answers.map((ans, index) => ` |
| 128 | + <li class="${ans.answer === ans.correctAnswer ? 'correct' : 'wrong'}"> |
| 129 | + <strong>Question ${index + 1}:</strong> ${ans.question} <br> |
| 130 | + <span>Your answer: ${ans.answer.toUpperCase()}</span> <br> |
| 131 | + <span>Correct answer: ${ans.correctAnswer.toUpperCase()}</span> |
| 132 | + </li> |
| 133 | + `).join('')} |
| 134 | + </ul> |
| 135 | + <button onclick="location.reload()">Replay</button> |
| 136 | + `; |
| 137 | +} |
| 138 | + |
0 commit comments