|
| 1 | + |
| 2 | +const questions = [ |
| 3 | + { |
| 4 | + questionName: 'question 1', |
| 5 | + answers: ['answer 1', 'answer 2', 'answer 3'], |
| 6 | + correctAnswers: [1] |
| 7 | + }, |
| 8 | + { |
| 9 | + questionName: 'question 2', |
| 10 | + answers: ['answer 1', 'answer 2', 'answer 3'], |
| 11 | + correctAnswers: [2] |
| 12 | + }, |
| 13 | + { |
| 14 | + questionName: 'question 3', |
| 15 | + answers: ['answer 1', 'answer 2', 'answer 3'], |
| 16 | + correctAnswers: [3] |
| 17 | + }, |
| 18 | +]; |
| 19 | + |
| 20 | +const app = { |
| 21 | + questions, |
| 22 | + testName: 'Тест по программированию', |
| 23 | + counter: 0, |
| 24 | + render() { |
| 25 | + const main = this.newEl('main'); |
| 26 | + const testName = this.newEl('h1'); |
| 27 | + testName.textContent = this.testName; |
| 28 | + const questionsList = this.newEl('ol'); |
| 29 | + |
| 30 | + /* |
| 31 | + * |
| 32 | + * this.renderQuestions() |
| 33 | + * |
| 34 | + * */ |
| 35 | + this.questions.forEach(question => { |
| 36 | + const li = this.newEl('li'); |
| 37 | + const questionHeader = this.newEl('h3'); |
| 38 | + questionHeader.textContent = question.questionName; |
| 39 | + |
| 40 | + questionHeader.setAttribute('class', 'title') |
| 41 | + const answers = this.newEl('ul'); |
| 42 | + |
| 43 | + question.answers.forEach((answer, answerIndex) => { |
| 44 | + answers.appendChild(this.renderAnswer(answer, answerIndex)); |
| 45 | + }); |
| 46 | + |
| 47 | + li.appendChild(questionHeader); |
| 48 | + li.appendChild(answers); |
| 49 | + questionsList.appendChild(li); |
| 50 | + }); |
| 51 | + const saveState = questions[1].questionName; |
| 52 | + const btn = this.newEl('button'); |
| 53 | + btn.textContent = 'Submit'; |
| 54 | + btn.setAttribute('type', 'submit'); |
| 55 | + btn.onclick = () => { |
| 56 | + const child = main.children[1]; |
| 57 | + const child2 = child.children; |
| 58 | + |
| 59 | + const titles = document.querySelectorAll('h3'); |
| 60 | + |
| 61 | + [...titles].forEach(h3 => { |
| 62 | + if(h3.getAttribute('class') === 'active') { |
| 63 | + h3.setAttribute('class', ''); |
| 64 | + } else { |
| 65 | + h3.setAttribute('class', 'active') |
| 66 | + } |
| 67 | + }); |
| 68 | + |
| 69 | +// const firstQuestion = child2[0].children[1].children[1].children[0]; |
| 70 | +// const secondQuestion = child2[1].children[1].children[1].children[0]; |
| 71 | +// const thirdQuestion = child2[2].children[1].children[1].children[0]; |
| 72 | +// |
| 73 | +// firstQuestion.checked = !firstQuestion.checked; |
| 74 | +// secondQuestion.checked = !secondQuestion.checked; |
| 75 | +// thirdQuestion.checked = !thirdQuestion.checked; |
| 76 | + |
| 77 | +// const answerFirst = child4.firstChild; |
| 78 | +// const answerLast = child4.lastChild; |
| 79 | +// |
| 80 | +// answerFirst.firstChild.checked = !answerFirst.firstChild.checked; |
| 81 | +// answerLast.firstChild.checked = !answerLast.firstChild.checked; |
| 82 | +// |
| 83 | +// this.counter++; |
| 84 | +// this.counter % 2 === 0 ? child3.textContent = saveState : child3.textContent = '999'; |
| 85 | + |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | + main.appendChild(testName); |
| 90 | + main.appendChild(questionsList); |
| 91 | + main.appendChild(btn); |
| 92 | + document.body.appendChild(main); |
| 93 | + }, |
| 94 | + renderAnswer(answer, answerIndex) { |
| 95 | + const li = this.newEl('li'); |
| 96 | + const label = this.newEl('label'); |
| 97 | + const uniqId = `uniq_${Math.random()}_${answerIndex}`; |
| 98 | + label.setAttribute('for', uniqId); |
| 99 | + label.textContent = answer; |
| 100 | + |
| 101 | + const input = this.newEl('input'); |
| 102 | + input.setAttribute('type', 'checkbox'); |
| 103 | + input.setAttribute('id', uniqId); |
| 104 | + |
| 105 | + li.appendChild(input); |
| 106 | + li.appendChild(label); |
| 107 | + return li; |
| 108 | + }, |
| 109 | + newEl(elName) { |
| 110 | + return document.createElement(elName); |
| 111 | + } |
| 112 | +}; |
| 113 | + |
| 114 | +app.render(); |
0 commit comments