quiz.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. var quiz = {
  2. // (A) PROPERTIES
  3. // (A1) QUESTIONS & ANSWERS
  4. // Q = QUESTION, O = OPTIONS, A = CORRECT ANSWER
  5. data: [
  6. {
  7. q : "What is the standard distance between the target and archer in Olympics?",
  8. o : [
  9. "50 meters",
  10. "70 meters",
  11. "100 meters",
  12. "120 meters"
  13. ],
  14. a : 1 // arrays start with 0, so answer is 70 meters
  15. },
  16. {
  17. q : "Which is the highest number on a standard roulette wheel?",
  18. o : [
  19. "22",
  20. "24",
  21. "32",
  22. "36"
  23. ],
  24. a : 3
  25. },
  26. {
  27. q : "How much wood could a woodchuck chuck if a woodchuck would chuck wood?",
  28. o : [
  29. "400 pounds",
  30. "550 pounds",
  31. "700 pounds",
  32. "750 pounds"
  33. ],
  34. a : 2
  35. },
  36. {
  37. q : "Which is the seventh planet from the sun?",
  38. o : [
  39. "Uranus",
  40. "Earth",
  41. "Pluto",
  42. "Mars"
  43. ],
  44. a : 0
  45. },
  46. {
  47. q : "Which is the largest ocean on Earth?",
  48. o : [
  49. "Atlantic Ocean",
  50. "Indian Ocean",
  51. "Arctic Ocean",
  52. "Pacific Ocean"
  53. ],
  54. a : 3
  55. }
  56. ],
  57. // (A2) HTML ELEMENTS
  58. hWrap: null, // HTML quiz container
  59. hQn: null, // HTML question wrapper
  60. hAns: null, // HTML answers wrapper
  61. // (A3) GAME FLAGS
  62. now: 0, // current question
  63. score: 0, // current score
  64. // (B) INIT QUIZ HTML
  65. init: function(){
  66. // (B1) WRAPPER
  67. quiz.hWrap = document.getElementById("quizWrap");
  68. // (B2) QUESTIONS SECTION
  69. quiz.hQn = document.createElement("div");
  70. quiz.hQn.id = "quizQn";
  71. quiz.hWrap.appendChild(quiz.hQn);
  72. // (B3) ANSWERS SECTION
  73. quiz.hAns = document.createElement("div");
  74. quiz.hAns.id = "quizAns";
  75. quiz.hWrap.appendChild(quiz.hAns);
  76. // (B4) GO!
  77. quiz.draw();
  78. },
  79. // (C) DRAW QUESTION
  80. draw: function(){
  81. // (C1) QUESTION
  82. quiz.hQn.innerHTML = quiz.data[quiz.now].q;
  83. // (C2) OPTIONS
  84. quiz.hAns.innerHTML = "";
  85. for (let i in quiz.data[quiz.now].o) {
  86. let radio = document.createElement("input");
  87. radio.type = "radio";
  88. radio.name = "quiz";
  89. radio.id = "quizo" + i;
  90. quiz.hAns.appendChild(radio);
  91. let label = document.createElement("label");
  92. label.innerHTML = quiz.data[quiz.now].o[i];
  93. label.setAttribute("for", "quizo" + i);
  94. label.dataset.idx = i;
  95. label.addEventListener("click", quiz.select);
  96. quiz.hAns.appendChild(label);
  97. }
  98. },
  99. // (D) OPTION SELECTED
  100. select: function(){
  101. // (D1) DETACH ALL ONCLICK
  102. let all = quiz.hAns.getElementsByTagName("label");
  103. for (let label of all) {
  104. label.removeEventListener("click", quiz.select);
  105. }
  106. // (D2) CHECK IF CORRECT
  107. let correct = this.dataset.idx == quiz.data[quiz.now].a;
  108. if (correct) {
  109. quiz.score++;
  110. this.classList.add("correct");
  111. } else {
  112. this.classList.add("wrong");
  113. }
  114. // (D3) NEXT QUESTION OR END GAME
  115. quiz.now++;
  116. setTimeout(function(){
  117. if (quiz.now < quiz.data.length) { quiz.draw(); }
  118. else {
  119. quiz.hQn.innerHTML = `You have answered ${quiz.score} of ${quiz.data.length} correctly.`;
  120. quiz.hAns.innerHTML = "";
  121. }
  122. }, 1000);
  123. }
  124. };
  125. window.addEventListener("load", quiz.init);