-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathappTest.js
More file actions
109 lines (91 loc) · 3.23 KB
/
Copy pathappTest.js
File metadata and controls
109 lines (91 loc) · 3.23 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
/* ТЕСТ */
/*
* Добавьте реальных вопросов про JavaScript с вариантами
* ответов
* */
// 3. При нажатии на кнопку если были выбраны правильные ответы,
// отображайте "ПРАВИЛЬНО" или не правильно
// или отображайте значек X или галочку, возле вопроса
const questions = [
{
questionName: 'question 1',
answers: ['answer 1_1', 'answer 1_2', 'answer 1_3'],
correctAnswersIndexes: [1]
},
{
questionName: 'question 2',
answers: ['answer 2_1', 'answer 2_2', 'answer 2_3'],
correctAnswersIndexes: [2]
},
{
questionName: 'question 3',
answers: ['answer 3_1', 'answer 3_2', 'answer 3_3'],
correctAnswersIndexes: [1, 2]
}
];
const findCorrectAnswer = answerToValidate => {
let correctAnswers = questions.map(question =>
question.correctAnswersIndexes.map(answerIndex => {
return question.answers[answerIndex];
})
);
return correctAnswers.some(answer => {
return answer.includes(answerToValidate);
});
};
const app = {
questions,
testName: 'Тест по программированию',
buttonName: 'Проверить',
render() {
const main = this.newEl('main');
const testName = this.newEl('h1');
testName.textContent = this.testName;
const questionsList = this.newEl('ol');
this.questions.forEach(question => {
const li = this.newEl('li');
const questionHeader = this.newEl('h3');
questionHeader.textContent = question.questionName;
const answers = this.newEl('ul');
question.answers.forEach((answer, answerIndex) => {
answers.innerHTML += this.renderAnswer(answer, answerIndex);
});
li.appendChild(questionHeader);
li.appendChild(answers);
questionsList.appendChild(li);
});
main.appendChild(testName);
main.appendChild(questionsList);
document.body.appendChild(main);
const button = this.newEl('button');
button.textContent = this.buttonName;
main.appendChild(button);
},
renderAnswer(answer, answerIndex) {
const uniqId = `uniq_${Math.random()}_${answerIndex}`;
return `
<li>
<input type="checkbox" id="${uniqId}">
<label for="${uniqId}">${answer}</label>
</li>
`
},
newEl(elName) {
return document.createElement(elName);
}
};
app.render();
const newButton = document.createElement('button');
newButton.textContent = 'Узнать ответы';
document.body.appendChild(newButton);
newButton.onclick = function () {
const allAnswer = document.body.querySelectorAll('label');
[...allAnswer].forEach( answer => {
const span = document.createElement('span');
let yourAnswer = answer.textContent;
let labelStatus = findCorrectAnswer(yourAnswer) ? 'CORRECT' : 'INCORRECT';
span.textContent = labelStatus;
answer.parentElement.insertBefore(span, answer);
});
console.log(allAnswer);
};