|
| 1 | + |
| 2 | +const questions = [ |
| 3 | + { |
| 4 | + questionName: 'question 1', |
| 5 | + answers: ['answer 1.1', 'answer 1.2', 'answer 1.3'], |
| 6 | + correctAnswersIndexes: [0], |
| 7 | + correctAnswers: ['answer 1.1'] |
| 8 | + }, |
| 9 | + { |
| 10 | + questionName: 'question 2', |
| 11 | + answers: ['answer 2.1', 'answer 2.2', 'answer 2.3'], |
| 12 | + correctAnswersIndexes: [1], |
| 13 | + correctAnswers: ['answer 2.2'] |
| 14 | + }, |
| 15 | + { |
| 16 | + questionName: 'question 3', |
| 17 | + answers: ['answer 3.1', 'answer 3.2', 'answer 3.3'], |
| 18 | + correctAnswersIndexes: [2], |
| 19 | + correctAnswers: ['answer 3.3'] |
| 20 | + }, |
| 21 | +]; |
| 22 | + |
| 23 | +const app = { |
| 24 | + questions, |
| 25 | + testName: 'Тест по программированию', |
| 26 | + counter: 0, |
| 27 | + render() { |
| 28 | + const main = this.newEl('main'); |
| 29 | + main.setAttribute('id', 'ads') |
| 30 | + const testName = this.newEl('h1'); |
| 31 | + testName.textContent = this.testName; |
| 32 | + const questionsList = this.newEl('ol'); |
| 33 | + |
| 34 | + /* |
| 35 | + * |
| 36 | + * this.renderQuestions() |
| 37 | + * |
| 38 | + * */ |
| 39 | + this.questions.forEach(question => { |
| 40 | + const li = this.newEl('li'); |
| 41 | + const questionHeader = this.newEl('h3'); |
| 42 | + questionHeader.textContent = question.questionName; |
| 43 | + |
| 44 | + questionHeader.setAttribute('class', 'title') |
| 45 | + const answers = this.newEl('ul'); |
| 46 | + |
| 47 | + question.answers.forEach((answer, answerIndex) => { |
| 48 | + answers.innerHTML += (this.renderAnswer(answer, answerIndex)); |
| 49 | + }); |
| 50 | + |
| 51 | + li.appendChild(questionHeader); |
| 52 | + li.appendChild(answers); |
| 53 | + questionsList.appendChild(li); |
| 54 | + }); |
| 55 | + |
| 56 | + const btn = this.newEl('button'); |
| 57 | + btn.textContent = 'Submit'; |
| 58 | + btn.setAttribute('type', 'submit'); |
| 59 | + |
| 60 | + |
| 61 | + main.appendChild(testName); |
| 62 | + main.appendChild(questionsList); |
| 63 | + main.appendChild(btn); |
| 64 | + document.body.appendChild(main); |
| 65 | + }, |
| 66 | + renderAnswer(answer, answerIndex) { |
| 67 | +// const li = this.newEl('li'); |
| 68 | +// li.setAttribute('class', 'dsa') |
| 69 | +// const label = this.newEl('label'); |
| 70 | + const uniqId = `uniq_${Math.random()}_${answerIndex}`; |
| 71 | +// label.setAttribute('for', uniqId); |
| 72 | +// label.textContent = answer; |
| 73 | +// |
| 74 | +// const input = this.newEl('input'); |
| 75 | +// input.setAttribute('type', 'checkbox'); |
| 76 | +// input.setAttribute('id', uniqId); |
| 77 | +// |
| 78 | +// li.appendChild(input); |
| 79 | +// li.appendChild(label); |
| 80 | +// return li; |
| 81 | + |
| 82 | + return ` |
| 83 | + <li> |
| 84 | + <input type="checkbox" id="${uniqId}"> |
| 85 | +
|
| 86 | + <label for="${uniqId}"> |
| 87 | + ${answer} |
| 88 | + </label> |
| 89 | + </li> |
| 90 | + ` |
| 91 | + |
| 92 | + }, |
| 93 | + newEl(elName) { |
| 94 | + return document.createElement(elName); |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +app.render(); |
| 99 | +//--------- |
| 100 | +const loading = document.querySelector('.main'); |
| 101 | +const testinsert = document.createElement('span'); |
| 102 | +testinsert.textContent = 'SPAN'; |
| 103 | + |
| 104 | +loading.parentElement.insertBefore(testinsert, loading); |
| 105 | + |
| 106 | +const testing = document.getElementById('ads'); |
| 107 | +const buttonSMTH = document.createElement('button'); |
| 108 | +buttonSMTH.setAttribute('type', 'submit'); |
| 109 | +buttonSMTH.textContent = 'Add SMTH' |
| 110 | + |
| 111 | +const answers = document.getElementsByClassName('dsa'); |
| 112 | + |
| 113 | +buttonSMTH.onclick = () => { |
| 114 | + |
| 115 | + questions.forEach((elem, index, arr) => { |
| 116 | + const correct = document.createElement('span'); |
| 117 | + const incorrect = document.createElement('span'); |
| 118 | + correct.textContent = 'correct'; |
| 119 | + incorrect.textContent = 'incorrect'; |
| 120 | + |
| 121 | + const answersFromDB = elem.answers; |
| 122 | + const rightAnswers = elem.correctAnswersIndexes; |
| 123 | + }); |
| 124 | + |
| 125 | +}; |
| 126 | + |
| 127 | +testing.appendChild(buttonSMTH); |
| 128 | + |
| 129 | +testing.insertAdjacentHTML('beforeend', '<button type="submit">QWERTY</button>'); |
| 130 | + |
| 131 | +const javaScriptOneLove = [ |
| 132 | + 'Arrow', |
| 133 | + 'Closure', |
| 134 | + 'Class' |
| 135 | +]; |
| 136 | + |
| 137 | +let createList = (arr) => { |
| 138 | + |
| 139 | + const renderItem = arr.map(item => `<li>${item}</li>`).join(''); |
| 140 | + |
| 141 | + return `<ul> |
| 142 | + ${renderItem} |
| 143 | + </ul>` |
| 144 | +} |
| 145 | + |
| 146 | +document.body.innerHTML += createList(javaScriptOneLove); |
0 commit comments