main.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. const qBase = [
  2. {
  3. question: 'Какой сейчас год?',
  4. a: '1000',
  5. b: '2000',
  6. c: '2001',
  7. d: '2021',
  8. answer: 'a'
  9. },
  10. {
  11. question: 'Самый распространенный язык программирования?',
  12. a: 'java script',
  13. b: 'java',
  14. c: 'python',
  15. d: 'c++',
  16. answer: 'b'
  17. },
  18. {
  19. question: 'Как расшифровывается аббревиатура HTML?',
  20. a: 'hello to media line',
  21. b: 'hypertext media language',
  22. c: 'hypertext markup language',
  23. d: 'hydro throne mobile loft',
  24. answer: 'c'
  25. },
  26. {
  27. question: 'Когда приходит Дед Мороз?',
  28. a: 'никогда',
  29. b: 'он и не уходил',
  30. c: 'в ночь перед Рождеством',
  31. d: 'в Новогоднюю ночь',
  32. answer: 'd'
  33. },
  34. ];
  35. // select
  36. const question = document.getElementById('question');
  37. const questionA = document.getElementById('guess_a');
  38. const questionB = document.getElementById('guess_b');
  39. const questionC = document.getElementById('guess_c');
  40. const questionD = document.getElementById('guess_d');
  41. const submitBtn = document.getElementById('submit');
  42. const result = document.getElementById('result');
  43. const radioButtons = document.querySelectorAll("input[type='radio']");
  44. // variables
  45. let currentQuestion = 0;
  46. let score = 0;
  47. // initial load
  48. loadQuestion();
  49. // =========== Functions ===============
  50. /**
  51. * set text from array
  52. * to corresponding HTML elements
  53. */
  54. function loadQuestion()
  55. {
  56. question.textContent = qBase[currentQuestion].question;
  57. questionA.textContent = qBase[currentQuestion].a;
  58. questionB.textContent = qBase[currentQuestion].b;
  59. questionC.textContent = qBase[currentQuestion].c;
  60. questionD.textContent = qBase[currentQuestion].d;
  61. }
  62. /**
  63. * for each radio button in collection
  64. * if checked
  65. * set id to variable answer
  66. * returns variable answer
  67. */
  68. function getSelectedRadioButtons()
  69. {
  70. let answer_id;
  71. radioButtons.forEach((button) =>
  72. {
  73. if (button.checked)
  74. answer_id = button.id;
  75. });
  76. return answer_id;
  77. }
  78. /**
  79. * for each radio button in collection
  80. * set checked state to false
  81. */
  82. function deselectRadioButtons()
  83. {
  84. radioButtons.forEach((button) => { button.checked = false; });
  85. }
  86. // =========== Events ===============
  87. submitBtn.addEventListener('click', () =>
  88. {
  89. const answer_id = getSelectedRadioButtons();
  90. if (answer_id == qBase[currentQuestion].answer)
  91. {
  92. score++;
  93. }
  94. if (currentQuestion >= (qBase.length - 1))
  95. {
  96. currentQuestion = 0;
  97. result.innerHTML = `<h1 style="font-size: 24px">Your score is: ${score} of ${qBase.length}</h1>`;
  98. }
  99. else
  100. {
  101. currentQuestion++;
  102. }
  103. deselectRadioButtons();
  104. loadQuestion();
  105. });