|
| 1 | +const quizes = [ |
| 2 | + { |
| 3 | + index: 0, |
| 4 | + content: "What is 5 times 8 ?", |
| 5 | + answers: {a:80, b:45, c:40, d:13}, |
| 6 | + correct: 'c' |
| 7 | + }, |
| 8 | + { |
| 9 | + index: 1, |
| 10 | + content: "What is 10 plus 5 ?", |
| 11 | + answers: {a: 50,b: 2,c: 5,d: 15}, |
| 12 | + correct: 'd' |
| 13 | + }, |
| 14 | + { |
| 15 | + index: 2, |
| 16 | + content: "What is the name of 3.14 ?", |
| 17 | + answers: {a: "Archimedes' constant π",b: "Euler's number e",c: "The imaginary unit i",d: "The golden ratio φ"}, |
| 18 | + correct: 'a' |
| 19 | + } |
| 20 | +]; |
| 21 | + |
| 22 | +let idx = 0; |
| 23 | +let point = 0; |
| 24 | + |
| 25 | +const resultModal = document.querySelector('.result-modal'); |
| 26 | +const question = document.querySelector('#question'); |
| 27 | +const options = document.querySelectorAll("input[type='radio']"); |
| 28 | +const allLabels = document.querySelectorAll("label"); |
| 29 | +const submit = document.querySelector("form input[type='submit']"); |
| 30 | + |
| 31 | +const answers = [...options]; |
| 32 | +const labels = [...allLabels]; |
| 33 | + |
| 34 | +//get question from array of objects |
| 35 | +let currentQuestion = quizes[idx]; |
| 36 | +//get the current question in html document |
| 37 | +let currentHtmlQuestion = answers[idx]; |
| 38 | + |
| 39 | +const getQuestions = () => { |
| 40 | + question.innerText = currentQuestion.content; |
| 41 | + labels[0].innerText = currentQuestion.answers.a; |
| 42 | + labels[1].innerText = currentQuestion.answers.b; |
| 43 | + labels[2].innerText = currentQuestion.answers.c; |
| 44 | + labels[3].innerText = currentQuestion.answers.d; |
| 45 | +} |
| 46 | + |
| 47 | +const removeChecked = () => { |
| 48 | + answers.forEach((opt) => { |
| 49 | + opt.checked = false; |
| 50 | + }); |
| 51 | +} |
| 52 | + |
| 53 | +getQuestions(); |
| 54 | + |
| 55 | +submit.addEventListener("click", (e) => { |
| 56 | + e.preventDefault(); |
| 57 | + idx ++; |
| 58 | + |
| 59 | + if(document.querySelector(`input[type='radio'][value='${currentQuestion.correct}']`).checked) { |
| 60 | + point ++; |
| 61 | + removeChecked(); |
| 62 | + } |
| 63 | + if(idx > 2) { |
| 64 | + getQuestions(); |
| 65 | + submit.value = "Finish Quiz"; |
| 66 | + removeChecked(); |
| 67 | + |
| 68 | + document.querySelector("#point").innerText = point; |
| 69 | + document.querySelector("#quizLength").innerText = quizes.length; |
| 70 | + resultModal.style.display = "block"; |
| 71 | + } else { |
| 72 | + currentQuestion = quizes[idx]; |
| 73 | + currentHtmlQuestion = answers[idx]; |
| 74 | + |
| 75 | + getQuestions(); |
| 76 | + |
| 77 | + if(document.querySelector(`input[type='radio'][value='${currentQuestion.correct}']`).checked) { |
| 78 | + point ++; |
| 79 | + } |
| 80 | + |
| 81 | + removeChecked(); |
| 82 | + } |
| 83 | + console.log(point); |
| 84 | +}); |
0 commit comments