|
| 1 | +const quizData = [ |
| 2 | + { |
| 3 | + question: "Which language runs in a web browser?", |
| 4 | + a: "Java", |
| 5 | + b: "C", |
| 6 | + c: "Python", |
| 7 | + d: "JavaScript", |
| 8 | + correct: "d", |
| 9 | + }, |
| 10 | + { |
| 11 | + question: "What does CSS stand for?", |
| 12 | + a: "Central Style Sheets", |
| 13 | + b: "Cascading Style Sheets", |
| 14 | + c: "Cascading Simple Sheets", |
| 15 | + d: "Cars SUVs Sailboats", |
| 16 | + correct: "b", |
| 17 | + }, |
| 18 | + { |
| 19 | + question: "What does HTML stand for?", |
| 20 | + a: "Hypertext Markup Language", |
| 21 | + b: "Hypertext Markdown Language", |
| 22 | + c: "Hyperloop Machine Language", |
| 23 | + d: "Helicopters Terminals Motorboats Lamborginis", |
| 24 | + correct: "a", |
| 25 | + }, |
| 26 | + { |
| 27 | + question: "What year was JavaScript launched?", |
| 28 | + a: "1996", |
| 29 | + b: "1995", |
| 30 | + c: "1994", |
| 31 | + d: "none of the above", |
| 32 | + correct: "b", |
| 33 | + }, |
| 34 | +]; |
| 35 | + |
| 36 | +const quiz = document.getElementById("quiz"); |
| 37 | +const answerElements = document.querySelectorAll(".answer"); |
| 38 | +const questionElement = document.getElementById("question"); |
| 39 | +const a_text = document.getElementById("a_text"); |
| 40 | +const b_text = document.getElementById("b_text"); |
| 41 | +const c_text = document.getElementById("c_text"); |
| 42 | +const d_text = document.getElementById("d_text"); |
| 43 | +const submitButton = document.getElementById("submit"); |
| 44 | + |
| 45 | +let currentQuiz = 0; |
| 46 | +let score = 0; |
| 47 | + |
| 48 | +const deselectAnswers = () => { |
| 49 | + answerElements.forEach((answer) => (answer.checked = false)); |
| 50 | +}; |
| 51 | + |
| 52 | +const getSelected = () => { |
| 53 | + let answer; |
| 54 | + answerElements.forEach((answerElement) => { |
| 55 | + if (answerElement.checked) answer = answerElement.id; |
| 56 | + }); |
| 57 | + return answer; |
| 58 | +}; |
| 59 | + |
| 60 | +const loadQuiz = () => { |
| 61 | + deselectAnswers(); |
| 62 | + const currentQuizData = quizData[currentQuiz]; |
| 63 | + questionElement.innerText = currentQuizData.question; |
| 64 | + a_text.innerText = currentQuizData.a; |
| 65 | + b_text.innerText = currentQuizData.b; |
| 66 | + c_text.innerText = currentQuizData.c; |
| 67 | + d_text.innerText = currentQuizData.d; |
| 68 | +}; |
| 69 | + |
| 70 | +loadQuiz(); |
| 71 | + |
| 72 | +submitButton.addEventListener("click", () => { |
| 73 | + const answer = getSelected(); |
| 74 | + if (answer) { |
| 75 | + if (answer === quizData[currentQuiz].correct) score++; |
| 76 | + currentQuiz++; |
| 77 | + if (currentQuiz < quizData.length) loadQuiz(); |
| 78 | + else { |
| 79 | + quiz.innerHTML = ` |
| 80 | + <h2>You answered ${score}/${quizData.length} questions correctly</h2> |
| 81 | + <button onclick="location.reload()">Reload</button> |
| 82 | + `; |
| 83 | + } |
| 84 | + } |
| 85 | +}); |
0 commit comments