Advertisement
Guest User

Untitled

a guest
May 21st, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.77 KB | None | 0 0
  1. const title = document.getElementById('title');
  2.  
  3. const start = document.getElementById('start');
  4.  
  5. const homepage = document.getElementById('homepage');
  6. const questions = document.getElementById('questions');
  7. const results = document.getElementById('results');
  8.  
  9. const openQuestions = document.getElementById('openQuestions');
  10. const choices = document.getElementById('choices');
  11.  
  12. const partyList = document.getElementById('partyList');
  13. const nextPage = document.getElementById('nextPage');
  14.  
  15. const question = document.getElementById('question');
  16. const statement = document.getElementById('statement');
  17.  
  18. const backButton = document.getElementById('backButton');
  19.  
  20. const isImportant = document.getElementById('isImportant');
  21.  
  22. const pro = document.getElementById('pro');
  23. const none = document.getElementById('none');
  24. const contra = document.getElementById('contra');
  25. const skip = document.getElementById('skip');
  26. const arrowNext = document.getElementById('arrowNext');
  27.  
  28. const match = document.getElementById('match');
  29.  
  30. const largeParties = document.getElementById('largeParties');
  31. const secularParties = document.getElementById('secularParties');
  32. const noParties = document.getElementById('noParties');
  33.  
  34. const firstPartyElement = document.getElementById('firstPartyElement');
  35. const secondPartyElement = document.getElementById('secondPartyElement');
  36. const thirdPartyElement = document.getElementById('thirdPartyElement');
  37.  
  38. const otherResults = document.getElementById('otherResults');
  39.  
  40. let answers = [];
  41. let counter = 0;
  42. let choicesStep = false;
  43.  
  44. let choicesLoaded = false;
  45.  
  46. window.onload = function () {
  47. startPage();
  48. }
  49.  
  50. // This is the function that starts onload that makes the homepage visable.
  51. function startPage() {
  52. homepage.style.display = 'block';
  53. questions.style.display = 'none';
  54. results.style.display = 'none';
  55.  
  56. backButton.style.display = 'none';
  57.  
  58. document.getElementById('questionCount').innerHTML = subjects.length;
  59.  
  60. start.addEventListener('click', startQuiz);
  61.  
  62. addValues();
  63. }
  64.  
  65. function addValues() {
  66. // This for loop gives all the parties a count and a mark property with a default value.
  67. for (let i = 0; i < parties.length; i++) {
  68. let party = parties[i];
  69. party['count'] = 0;
  70. party['marked'] = false;
  71. }
  72. }
  73.  
  74. // This function contains the question display and calls the 'setStatement()' function to place the chosen aswer.
  75. function startQuiz() {
  76.  
  77. homepage.style.display = 'none';
  78. questions.style.display = 'block';
  79. results.style.display = 'none';
  80.  
  81. backButton.style.display = 'block';
  82.  
  83. openQuestions.style.display = 'block';
  84. choices.style.display = 'none';
  85.  
  86. pro.addEventListener('click', setAnswer);
  87. none.addEventListener('click', setAnswer);
  88. contra.addEventListener('click', setAnswer);
  89. skip.addEventListener('click', setAnswer);
  90.  
  91. backButton.addEventListener('click', goBack);
  92.  
  93. setStatement();
  94. }
  95.  
  96. // This function makes it possible to navigate back through the questions.
  97. function goBack() {
  98. // when the counter is 0 it means that there are no more questions to go back to so the homepage has to be shown.
  99. if (counter === 0) {
  100. // back to homepage
  101. startPage();
  102.  
  103. // when the counter is the same number as there are subjects, it means that you wanna go back to the last question from the choices screen.
  104. } else if (counter === subjects.length) {
  105. startQuiz();
  106.  
  107. // if both conditions are not true, then the counter loses 1 so you go back just 1 question.
  108. } else {
  109. counter--;
  110. startQuiz();
  111. }
  112. }
  113.  
  114. // This function checks the eventListner and the important checkbox.
  115. // It adds the id of the pressed button (pro,none,contra or skip) and the 'checked' to the answers array.
  116. function setAnswer(value) {
  117. answers[counter] = {
  118. "position": value.target.id,
  119. "isImportant": isImportant.checked
  120. };
  121.  
  122. counter++;
  123. setStatement();
  124. }
  125.  
  126. // This function contains a if else to check by the counter which question needs to be displayed, if its the last one there will be linked to showChoices().
  127. function setStatement() {
  128. if (counter === subjects.length && choicesStep === false) {
  129. showChoices();
  130.  
  131. } else if (counter === subjects.length && choicesStep === true) {
  132. counter = subjects.length;
  133. counter--;
  134. setStatement();
  135. } else if (counter < subjects.length) {
  136.  
  137. question.innerHTML = counter + 1 + '. ' + subjects[counter].title;
  138. statement.innerHTML = subjects[counter].statement;
  139.  
  140. choicesStep = false;
  141. checkStatement();
  142. }
  143. }
  144.  
  145. // This function makes it possible to remember if a question is marked as important.
  146. function checkStatement() {
  147. if (answers[counter] == undefined) {
  148. isImportant.checked = false;
  149. } else {
  150. isImportant.checked = answers[counter].isImportant;
  151. }
  152. }
  153.  
  154. // This function create a page where there has to be chosen a preference for at least 3 parties.
  155. function showChoices() {
  156. choicesStep = true;
  157.  
  158. openQuestions.style.display = 'none';
  159. choices.style.display = 'block';
  160.  
  161. backButton.addEventListener('click', goBack);
  162.  
  163. question.innerHTML = 'Welke partijen wilt u meenemen in het resultaat?';
  164. statement.innerHTML = 'U kunt kiezen voor grote partijen, daarbij nemen we de partijen mee die in de peilingen op minimaal één zetel staan. U kunt kiezen voor seculiere partijen. Maak tenminste een selectie van drie partijen.';
  165.  
  166. nextPage.addEventListener('click', result);
  167.  
  168. createParties();
  169.  
  170. largeParties.addEventListener('click', largePartiesSelection);
  171. secularParties.addEventListener('click', secularPartiesSelection);
  172. noParties.addEventListener('click', noPartiesSelection);
  173. }
  174.  
  175. // This function creates the list of parties that are available, it also gives them checkboxes.
  176. function createParties() {
  177. if (!choicesLoaded) {
  178.  
  179. for (let i = 0; i < parties.length; i++) {
  180. const currIndex = i;
  181.  
  182. let createLi = document.createElement('li');
  183. createLi.className = "list-group-item";
  184.  
  185. let checkbox = document.createElement('input');
  186. checkbox.setAttribute('type', 'checkbox');
  187. checkbox.setAttribute('id', parties[i].name);
  188. checkbox.onclick = function () {
  189. setParty(currIndex);
  190. };
  191. createLi.appendChild(checkbox);
  192.  
  193. let liContent = document.createTextNode(parties[i].name);
  194. createLi.appendChild(liContent);
  195.  
  196. partyList.appendChild(createLi);
  197. }
  198. choicesLoaded = true;
  199. }
  200. }
  201.  
  202. // This function creates a selection of all parties with more then 1 seat.
  203. function largePartiesSelection() {
  204. for (let i = 0; i < parties.length; i++) {
  205. let party = parties[i];
  206. if (party.size >= 1) {
  207. party.marked = true;
  208.  
  209. let partyName = party.name;
  210. document.getElementById(partyName).checked = true;
  211. }
  212. }
  213. }
  214.  
  215. // This function creates a selection of all secular parties.
  216. function secularPartiesSelection() {
  217. for (let i = 0; i < parties.length; i++) {
  218. let partySecular = parties[i].secular;
  219. if (partySecular) {
  220. let party = parties[i];
  221. party.marked = true;
  222.  
  223. let partyName = party.name;
  224. document.getElementById(partyName).checked = true;
  225. }
  226. }
  227. }
  228.  
  229. // This function clears the selection.
  230. function noPartiesSelection() {
  231. for (let i = 0; i < parties.length; i++) {
  232. let party = parties[i];
  233. party['marked'] = false;
  234.  
  235. let partyErase = parties[i].name;
  236.  
  237. document.getElementById(partyErase).checked = false;
  238. }
  239. }
  240.  
  241. // This function contains a check to see if the party is added to a array.
  242. function setParty(index) {
  243. if (parties[index].marked) {
  244. parties[index].marked = false;
  245. } else {
  246. parties[index].marked = true;
  247. }
  248. }
  249.  
  250. // This function creates a page where the results are displayed.
  251. function result() {
  252. var markCounter = 0;
  253. for (let k = 0; k < parties.length; k++) {
  254. if (parties[k].marked) {
  255. markCounter++;
  256. }
  257. }
  258.  
  259. if (markCounter >= 3) {
  260.  
  261. choicesStep = false;
  262.  
  263. homepage.style.display = 'none';
  264. questions.style.display = 'none';
  265. results.style.display = 'block';
  266.  
  267. backButton.addEventListener('click', goBack);
  268.  
  269. checkAnswer();
  270. showResult();
  271. } else {
  272. alert('Selecteer ten minste 3 partijen.');
  273. }
  274. }
  275.  
  276. // When the last answer is filled, this function looks at all the parties which answers are the same.
  277. // When a answer is the same as the one of the party, the party gets +1 by the count unless the answer is set to important, then it gets +2.
  278. function checkAnswer() {
  279. // This for loop loops as many times as there are subjects. Every loop is one answer which will be checked individually.
  280. for (let i = 0; i < subjects.length; i++) {
  281.  
  282. // Every position inside the answers array will be placed in answerPosition.
  283. // Each answer will be checked after each other because of the i value(the loop value).
  284. let answerPosition = answers[i].position;
  285. // If a answer contains a 'skip' value the loop will be stoped and the next loop will start.
  286. if (answerPosition === "skip" || answerPosition === "arrowNext") {
  287.  
  288. // Continue means that the for loop will go to the next loop instead of break what stops the for loop instantly.
  289. continue;
  290. }
  291.  
  292. // Every isImportant inside the answers array will be placed in answerIsImportant.
  293. // Each answer will be checked after each other because of the i value(the loop value).
  294. let answerIsImportant = answers[i].isImportant;
  295.  
  296. // Every subject will be played individually inside the subject array be the value of i.
  297. let subject = subjects[i];
  298.  
  299.  
  300. // this loop makes it possible to take each party individually out of the subject array.
  301. for (let j = 0; j < subject.parties.length; j++) {
  302.  
  303. // Every loop the party will be placed inside subjectParty for a better readability.
  304. let subjectParty = subject.parties[j];
  305.  
  306. // Every loop the position of the party will be placed in subjectPartyPosition for a better readability.
  307. let subjectPartyPosition = subjectParty.position;
  308.  
  309. // this condition checks if the position of the party that is being checked is the same as the filled answer.
  310. if (subjectPartyPosition === answerPosition) {
  311.  
  312. // This loop checks all parties for a match. When there is a match it will look for the isImportant so the right value can be added.
  313. for (let k = 0; k < parties.length; k++) {
  314.  
  315. let party = parties[k];
  316.  
  317. // If the party.name(parties array) has the same value as subjectParty(subjects array) the count will add with the right value.
  318. if (party.name === subjectParty.name) {
  319.  
  320. // If the answer is not important the count will be +1.
  321. if (!answerIsImportant) {
  322. party.count += 1;
  323. // If the answer is important the count will be +2.
  324. } else if (answerIsImportant) {
  325. party.count += 2;
  326. }
  327.  
  328. // The break will make sure the loop will stop immediately so the next subjectParty can be checked.
  329. // This break will only work when the party.name and subjectParty.name are the same.
  330. break;
  331. }
  332. }
  333. }
  334. }
  335.  
  336. }
  337. }
  338.  
  339. // This function creates the result page
  340. function showResult() {
  341. // Step 1:
  342. // var checked = Array.from(document.querySelectorAll('.list-group-item > input[type="checkbox"]'))
  343. // .filter(function(checkbox) {
  344. // return checkbox.checked === false }
  345. // );
  346.  
  347. // var filteredParties = parties.filter(function(party) {
  348. // return checked.find(function(checked) {
  349. // return checked.id === party.name
  350. // });
  351. // });
  352.  
  353. // filteredParties.sort()
  354.  
  355.  
  356. // return;
  357.  
  358. otherResults.innerHTML = '';
  359.  
  360. let markedParties = [];
  361. // Go trough all of the parties.
  362. for (let i = 0; i < parties.length; i++) {
  363.  
  364. var party = parties[i];
  365. // If a party is marked, we want to add it to markedParties.
  366. if (party.marked) {
  367. // markedParties[i] = party;
  368. markedParties.push(party);
  369. }
  370.  
  371. }
  372.  
  373. // This inside sort function makes sure all the data is sorted by the count from high to low value.
  374. markedParties.sort(function (a, b) {
  375. return b.count - a.count;
  376. });
  377.  
  378. match.innerHTML = '1# ' + markedParties[0].name + ' with a score of ' + markedParties[0].count;
  379.  
  380. // These are the first 3 value's from the array which means that these had the most similar answers.
  381. firstPartyElement.innerHTML = '1# ' + markedParties[0].name + '<p><b> with score: </b></p> ' + markedParties[0].count;
  382. secondPartyElement.innerHTML = '2# ' + markedParties[1].name + '<p><b> with score: </b></p> ' + markedParties[1].count;
  383. thirdPartyElement.innerHTML = '3# ' + markedParties[2].name + '<p><b> with score: </b></p> ' + markedParties[2].count;
  384.  
  385. for (let i = 3; i < markedParties.length; i++) {
  386. let li = document.createElement('li');
  387. li.innerHTML = markedParties[i].name + '<p><b> with score: </b></p> ' + markedParties[i].count;
  388.  
  389. // The li will be placed inside the ul (otherResults).
  390. otherResults.appendChild(li);
  391. }
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement