-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
91 lines (84 loc) · 3.1 KB
/
script.js
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
// TODO(you): Write the JavaScript necessary to complete the homework.
// You can access the RESULTS_MAP from "constants.js" in this file since
// "constants.js" has been included before "script.js" in index.html.
function highlightChoice(checkedChoice) {
const parentSection = checkedChoice.parentNode;
for (const choice of parentSection.querySelectorAll('div')) {
if (choice !== checkedChoice) {
choice.classList.remove('highlighted');
choice.classList.add('unhighlighted');
choice.querySelector(".checkbox").src = "images/unchecked.png";
}
}
checkedChoice.classList.remove('unhighlighted');
checkedChoice.classList.add('highlighted');
const checkedCheckbox = checkedChoice.querySelector('.checkbox');
checkedCheckbox.src = "images/checked.png";
}
function restartQuiz() {
console.log('quiz restarted');
sectionsAvailable = [];
for (const choice of sectionChoices) {
choice.addEventListener('click', recordChoice);
choice.classList.add('.unanswered');
choice.classList.remove('highlighted');
choice.classList.remove('unhighlighted');
choice.querySelector(".checkbox").src = "images/unchecked.png";
}
if (document.querySelector('#resultSection') !== null) {
document.querySelector('#resultSection').innerHTML = '';
}
}
function printQuizResult(title, text) {
const resultSection = document.querySelector('#resultSection');
const resultDiv = document.createElement('div')
const resultTitle = document.createElement('h1');
resultTitle.textContent = title;
const resultText = document.createElement('p');
resultText.textContent= text;
const restartButton = document.createElement('div');
restartButton.textContent = "Restart Quiz";
restartButton.addEventListener('click', restartQuiz);
restartButton.id="restartButton";
resultDiv.appendChild(resultTitle);
resultDiv.appendChild(resultText);
resultSection.appendChild(resultDiv);
resultSection.appendChild(restartButton);
for (const element of resultSection.querySelectorAll("div, p, h1")) {
element.classList.add("resultElement");
}
}
function calculateResult (choicesDict, one, two, three) {
if (choicesDict[three] === choicesDict[two]) {
return RESULTS_MAP[choicesDict[two]];
} else {
return RESULTS_MAP[choicesDict[one]];
}
}
function quizCompleted() {
console.log('Quiz Completed!');
for (const choice of sectionChoices) {
choice.removeEventListener('click', recordChoice);
}
const result = calculateResult(choicesDict, 'one', 'two', 'three');
printQuizResult(result['title'], result['contents']);
}
function recordChoice(event) {
const choice = event.currentTarget;
choicesDict[choice.dataset.questionId]=choice.dataset.choiceId;
sectionOfChoice = choice.parentNode;
highlightChoice(choice);
for (const completedSection of sectionsAvailable) {
if (completedSection === sectionOfChoice) {
return;
}
}
sectionsAvailable.push(sectionOfChoice);
if (sectionsAvailable.length >= 3) {
quizCompleted();
}
}
const sectionChoices = document.querySelectorAll('.choice-grid div');
const choicesDict = {}
let sectionsAvailable = [];
restartQuiz();