-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
127 lines (115 loc) · 3.86 KB
/
main.js
File metadata and controls
127 lines (115 loc) · 3.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const app = {
questions : [
{
id : 1,
question : 'Сколько типов данных в JS',
answers: [
'5',
'6',
'7'
],
correctAnswer : ['6']
},
{
id : 2,
question : 'Методом чего является функция slice',
answers: [
'массив',
'число',
'объект'
],
correctAnswer : ['массив']
},
{
id : 3,
question : 'var y = 1, x = y = typeof x; x;',
answers: [
'1',
'number',
'undefined'
],
correctAnswer : ['undefined']
}
],
_createButton(){
const buttonSubmit = this._makeNode('input');
buttonSubmit.type = 'submit';
buttonSubmit.name = 'submit';
buttonSubmit.value = 'Проверить мои рещультаты';
return buttonSubmit;
},
_makeNode(node){
return document.createElement(node);
},
_createListOfQuestions(){
const questionList = this._makeNode('ol');
this.questions.forEach((questionObj,questionIndex)=>{
const questionBlock = this._makeNode('li');
questionBlock.innerHTML = `${questionObj.question} <span class="status"></span>`;
const answersBlock = this._makeNode('ul');
questionObj.answers.forEach( (answerText,index) => {
const inputAnswer = this._makeNode('input');
inputAnswer.type = 'checkbox';
inputAnswer.name = `question_${questionObj.id}_answer_${index}` ;
const labelForAnswer = this._makeNode('label');
labelForAnswer.appendChild(inputAnswer);
const spanForAnswer = this._makeNode('span');
spanForAnswer.textContent = answerText
labelForAnswer.appendChild(spanForAnswer);
const answerLi = this._makeNode('li');
answerLi.appendChild(labelForAnswer);
answersBlock.appendChild(answerLi);
});
questionBlock.appendChild(answersBlock);
questionList.appendChild(questionBlock);
});
return questionList;
},
_initContainer(){
const div = this._makeNode('div');
div.setAttribute("class", "test-block");
return div;
},
_createHead(){
const head = this._makeNode('p');
head.innerText = 'Тест по програмированию'
return head
},
checkAnswer(){
const questionsElements = document.body.querySelectorAll('ol ul');
questionsElements.forEach( (element, index) => {
let haveMistakes = true;
const questionNumber = element.querySelector('label > input').name.split('_')[1];
const answersCkeckboxes = element.querySelectorAll('li > label > input:checked');
const questionWeWork = this.questions.filter( question => {
return question.id == questionNumber;
})[0];
if(answersCkeckboxes.length){
haveMistakes = [...answersCkeckboxes].some( answersCkeckbox => {
const answerText = answersCkeckbox.nextElementSibling.innerText;
return questionWeWork.correctAnswer.indexOf(answerText) === -1;
});
}
const newStatus = 'status' + (haveMistakes ? ' wrong' : ' correct');
element.parentElement.querySelector('span.status').className = newStatus
});
},
render(){
const container = this._initContainer();
const formHeader = this._createHead();
const form = this._makeNode('form');
const questionList = this._createListOfQuestions();
const submitButton = this._createButton();
submitButton.onclick = () => {
this.checkAnswer();
return false;
}
const buttonBlock = this._makeNode('p');
buttonBlock.appendChild(submitButton);
form.appendChild(questionList).appendChild(buttonBlock);
container.appendChild(formHeader);
container.appendChild(form);
document.body.appendChild(container);
}
}
app.render();