quizConstructor.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const yourQuiz = []
  2. const createQuizBtn = document.getElementById('create_constructor')
  3. createQuizBtn.onclick = (e) => {
  4. e.preventDefault();
  5. e.stopPropagation();
  6. document.querySelector('#button-block').setAttribute('display', 'none');
  7. buildConstructor();
  8. }
  9. const titleOfQuiz = () => {
  10. const header = document.querySelector('.main-header')
  11. const userQuizTitle = document.createElement('h2')
  12. }
  13. const buildConstructor = () => {
  14. const templateForm = document.createElement('form')
  15. templateForm.className = "template_form"
  16. templateForm.innerHTML = `<fieldset>
  17. <legend>Quiz Constructor</legend>
  18. <p><label for="titleQuiz">Quiz Title</label><input type="text" id="titleQuiz" placeholder="Enter the title of your Quiz"></p>
  19. <p><label for="customerQuestion">Question</label><input type="text" id="customerQuestion" placeholder="Enter a question"></p>
  20. <div class="constructor_answers">
  21. <p><label>Answers</label></p>
  22. <p>
  23. <input type="text" id="answer_letter" class="constructor_answer_letter" placeholder="Enter a letter(ex: a or b or c or d)">
  24. <input type="text" class="constructor_answer" placeholder="Enter an answer"></p>
  25. </div>
  26. <div>
  27. <button type="button" class="add_answer">Add answer</button>
  28. </div>
  29. <p><label for="correct_answer_letter">Enter the letter of the correct answer(ex: a or b or c or d)</label>
  30. <input type="text" id="correct_answer_letter" class="constructor_correct_answer_lettter" placeholder="Enter the letter of the correct answer"></p>
  31. </fieldset>
  32. <button type="submit" class="add_question" id="add_question">Add a question</button>
  33. <button type="submit" class="start_your_quiz" id="start_your_quiz">Start Your Quiz</button>
  34. `
  35. document.querySelector('#quiz_constructor').appendChild(templateForm);
  36. document.querySelector('.add_answer').onclick = (e) => {
  37. e.preventDefault();
  38. e.stopPropagation();
  39. let answerContainer = document.querySelector('.constructor_answers');
  40. const frame = document.createElement('p');
  41. frame.innerHTML = `
  42. <input type="text" id="answer_letter" class="constructor_answer_letter" placeholder="Enter a letter">
  43. <input type="text" class="constructor_answer" placeholder="Enter an answer">`
  44. answerContainer.appendChild(frame);
  45. }
  46. document.querySelector('.add_question').onclick = (e) => {
  47. e.preventDefault();
  48. e.stopPropagation();
  49. yourQuiz.push(newQuestion())
  50. setConstructorToLS(yourQuiz);
  51. templateForm.remove();
  52. buildConstructor();
  53. }
  54. console.log(yourQuiz)
  55. const btnStartYourQuiz = document.getElementById('btnStart');
  56. btnStartYourQuiz.onclick = (e) => {
  57. e.preventDefault();
  58. e.stopPropagation();
  59. setConstructorToLS();
  60. templateForm.remove();
  61. initializeClock('countdown', deadline);
  62. buildQuiz();
  63. localStorage.setItem('timeinterval', initializeClock('countdown', deadline));
  64. }
  65. }
  66. titleQuiz = document.querySelector('#titleQuiz')
  67. const newQuestion = () => {
  68. const questionCust = document.querySelector('#customerQuestion').value;
  69. const letterCust = document.querySelector('.constructor_answer_letter').value;
  70. const answerCust = document.querySelector('.constructor_answer').value;
  71. const correctAnswerLetter = document.querySelector('#correct_answer_letter').value;
  72. let answers = []
  73. for (letterCust in questionCust.answers) {
  74. answerCust = questionCust.answers[letterCust]
  75. answers.push(`${letterCust} : ${answerCust}`)
  76. }
  77. return {
  78. question: questionCust,
  79. answers: {...[answers].map(item => item.key)},
  80. correctAnswer: correctAnswerLetter
  81. }
  82. }