-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
174 lines (146 loc) · 4.87 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// 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.
const unchecked_image = "images/unchecked.png"
const checked_image = "images/checked.png"
let answers =[];
let finalPicks = new Map();
function showObject(obj) {
var result = "";
for (var p in obj) {
if( obj.hasOwnProperty(p) ) {
result += p + " , " + obj[p] + "\n";
}
}
return result;
}
function changeImage(choice, start, end) {
const image = choice.querySelector('.checkbox');
if(choice.style.backgroundColor !== '#cfe3ff') {
if(choice.style.opacity === '0.6') {
choice.style.opacity = '1.0';
}
choice.style.backgroundColor = '#cfe3ff';
image.src = checked_image;
for(let i = start; i<end; i++) {
let other = answerChoices[i];
if(other!==choice) {
other.style.opacity='0.6';
other.style.backgroundColor = '#f4f4f4';
other.querySelector('.checkbox').src = unchecked_image;
}
}
}
}
function addAnswers(choice) {
if(!answers.includes(choice.dataset.questionId)) {
answers.push(choice.dataset.questionId);
}
}
function scrollToNext(nextID) {
document.getElementById(nextID).scrollIntoView({behavior: "smooth"});
}
function answerQuestions(choice) {
if(choice.dataset.questionId === 'one') {
changeImage(choice, 0, 7);
addAnswers(choice);
scrollToNext('two');
} else if(choice.dataset.questionId === 'two') {
changeImage(choice, 7, 15);
addAnswers(choice);
scrollToNext('three');
} else if(choice.dataset.questionId === 'three') {
changeImage(choice, 15, 23);
addAnswers(choice);
scrollToNext('four');
} else if(choice.dataset.questionId === 'four') {
changeImage(choice, 23, 31);
addAnswers(choice);
scrollToNext('five');
} else if(choice.dataset.questionId === 'five') {
changeImage(choice, 31, 39);
addAnswers(choice);
scrollToNext('six');
} else if(choice.dataset.questionId === 'six') {
changeImage(choice, 39, 47);
addAnswers(choice);
scrollToNext('seven');
} else if(choice.dataset.questionId === 'seven') {
changeImage(choice, 47, 55);
addAnswers(choice);
scrollToNext('eight');
} else if(choice.dataset.questionId === 'eight') {
changeImage(choice, 55, 63);
addAnswers(choice);
scrollToNext('nine');
} else if(choice.dataset.questionId === 'nine') {
changeImage(choice, 63, 71);
addAnswers(choice);
scrollToNext('ten');
} else {
changeImage(choice, 71, 79);
addAnswers(choice);
}
}
function selectItem(event) {
answerQuestions(event.currentTarget);
if(answers.length === 10) {
getResults();
displayResults();
}
}
function getResults() {
for(let i = 0; i<answerChoices.length; i++) {
let element = answerChoices[i];
if(element.style.backgroundColor === 'rgb(207, 227, 255)') {
finalPicks.set(element.dataset.questionId, element.dataset.choiceId);
}
}
}
const resultContainer = document.querySelector('#result');
function getText(title, body, result, letter, key) {
title.textContent = "You got:" + ' ' + RESULTS_MAP[key]['title'];
body.textContent = RESULTS_MAP[key]['contents'];
result.src = 'images/' + letter + '.jpg';
resultContainer.classList.remove("hidden");
scrollToNext('end');
}
function displayResults() {
//console.log(finalPicks);
const title = document.querySelector('#title');
const body = document.querySelector('#bodytext p');
const result = document.querySelector('#result_img');
// Find the answer with the most elements
var counts = {};
finalPicks.forEach(function(x) { counts[x] = (counts[x] || 0)+1; });
//console.log(counts);
var keys = keys = Object.keys(counts);
var highest = keys.sort(function(a,b){return counts[b]-counts[a]});
var winner = highest[0];
console.log('The letter ' + highest[0] + ' had the most answers, so the recommended study area is ' + RESULTS_MAP[winner]['title']);
getText(title, body, result, highest[0], winner)
for(const option of answerChoices) {
option.removeEventListener('click', selectItem);
}
}
function refresh() {
resultContainer.classList.add("hidden");
for(let i = 0; i<answerChoices.length; i++) {
let element = answerChoices[i];
const image = element.querySelector('.checkbox');
element.addEventListener('click', selectItem);
element.style.opacity = '1.0';
element.style.backgroundColor = '#f4f4f4';
image.src = unchecked_image;
}
answers = [];
finalPicks.clear();
scrollToNext('body_start');
}
let restartQuiz = document.querySelector('#result');
restartQuiz.addEventListener('click', refresh);
let answerChoices = document.querySelectorAll('.choice-grid div');
for(let i = 0; i<answerChoices.length; i++) {
let element = answerChoices[i];
element.addEventListener('click', selectItem);
}