quizConstructor.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. const yourQuiz = []
  2. const buildConstructor = () => {
  3. const templateForm = document.createElement('form')
  4. templateForm.className = "template_form"
  5. templateForm.innerHTML = `<fieldset>
  6. <legend>Quiz Constructor</legend>
  7. <p><label for="titleQuiz">Quiz Title</label><input type="text" id="titleQuiz" placeholder="Enter the title of your Quiz"></p>
  8. <p><label for="customerQuestion">Question</label><input type="text" id="customerQuestion" placeholder="Enter a question"></p>
  9. <div class="constructor_answers">
  10. <p><label>Answers</label></p>
  11. <p>
  12. <input type="text" id="answer_letter" class="constructor_answer_lettter" placeholder="Enter a letter(ex: a or b or c or d)">
  13. <input type="text" class="constructor_answer" placeholder="Enter an answer"></p>
  14. </div>
  15. <div>
  16. <button type="button" class="add_answer">Add answer</button>
  17. </div>
  18. <p><label for="correct_answer_letter">Enter the letter of the correct answer(ex: a or b or c or d)</label>
  19. <input type="text" id="correct_answer_letter" class="constructor_correct_answer_lettter" placeholder="Enter the letter of the correct answer"></p>
  20. </fieldset>
  21. <button type="submit" class="add_question" id="add_question">Add a question</button>
  22. `
  23. document.querySelector('#quiz_constructor').appendChild(templateForm);
  24. document.querySelector('.add_answer').onclick = (e) => {
  25. e.preventDefault();
  26. e.stopPropagation();
  27. let answerContainer = document.querySelector('.constructor_answers');
  28. const frame = document.createElement('p');
  29. frame.innerHTML = `
  30. <input type="text" id="answer_letter" class="constructor_answer_lettter" placeholder="Enter a letter">
  31. <input type="text" class="constructor_answer" placeholder="Enter an answer">`
  32. answerContainer.appendChild(frame);
  33. }
  34. document.querySelector('.add_question').onclick = (e) => {
  35. e.preventDefault();
  36. e.stopPropagation();
  37. const questionCust = document.querySelector('#customerQuestion').value;
  38. const letterCust = document.querySelector('.constructor_answer_lettter').value;
  39. const answerCust = document.querySelector('.constructor_answer').value;
  40. yourQuiz.push(new Question(questionCust, letterCust, answerCust))
  41. setConstructorToLS(yourQuiz);
  42. templateForm.remove();
  43. buildConstructor();
  44. }
  45. console.log(yourQuiz)
  46. }
  47. titleQuiz = document.querySelector('#titleQuiz')
  48. class Question {
  49. constructor(questionCust, answerCust, letterCust) {
  50. this.question = questionCust;
  51. this.answers = {letterCust: answerCust};
  52. this.correctAnswer = document.querySelector('#correct_answer_letter').value;
  53. }
  54. }
  55. createQuizBtn.onclick = (e) => {
  56. e.preventDefault();
  57. e.stopPropagation();
  58. document.querySelector('#button-block').setAttribute('display', 'none');
  59. buildConstructor();
  60. }