Advertisement
shady_obeyd

6.Softuni Quiz

Jan 24th, 2019
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     let buttons = Array.from(document.getElementsByTagName('button'));
  3.  
  4.     let answerCounter = 0;
  5.  
  6.     for(let button of buttons){
  7.         button.addEventListener('click', function listen(e) {
  8.             let buttonTarget = e.target;
  9.  
  10.             let parent = buttonTarget.parentNode;
  11.  
  12.             let firstOption = parent.children[1];
  13.             let secondOption = parent.children[4];
  14.             let thirdOption = parent.children[7];
  15.             let fourthOption = parent.children[10];
  16.  
  17.             checkOption(firstOption);
  18.             checkOption(secondOption);
  19.             checkOption(thirdOption);
  20.             checkOption(fourthOption);
  21.  
  22.             let checkedOptionValue = '';
  23.  
  24.             if(firstOption.checked){
  25.                 checkedOptionValue = firstOption.value;
  26.                 disableOptions(secondOption, thirdOption, fourthOption);
  27.             } else if(secondOption.checked){
  28.                 checkedOptionValue = secondOption.value;
  29.                 disableOptions(firstOption, thirdOption, fourthOption);
  30.             } else if(thirdOption.checked){
  31.                 checkedOptionValue = thirdOption.value;
  32.                 disableOptions(firstOption, secondOption, fourthOption);
  33.             } else if(fourthOption.checked){
  34.                 checkedOptionValue = fourthOption.value;
  35.                 disableOptions(firstOption, secondOption, thirdOption);
  36.             }
  37.  
  38.             if(checkedOptionValue === ''){
  39.                 return;
  40.             }
  41.  
  42.             let question = parent.children[0].textContent;
  43.  
  44.             if(question === 'When SoftUni was created?'){
  45.                 if(checkedOptionValue === '2013'){
  46.                     answerCounter++;
  47.                 }
  48.             } else if(question === 'Which is the most popular name in the SoftUni?'){
  49.                 if(checkedOptionValue === 'Pesho'){
  50.                     answerCounter++;
  51.                 }
  52.             } else if(question === 'Which of the following names is the founder of the SoftUni?'){
  53.                 if(checkedOptionValue === 'Nakov'){
  54.                     answerCounter++;
  55.                 }
  56.             }
  57.  
  58.             let buttonIndex = buttons.indexOf(button);
  59.  
  60.             if(buttonIndex === 0 || buttonIndex === 1){
  61.                 let nextSection = document.getElementsByTagName('section')[buttonIndex + 1];
  62.  
  63.                 nextSection.className = '';
  64.             } else if(buttonIndex === 2){
  65.                 let result = document.getElementById('result');
  66.                 if(answerCounter === 3){
  67.                     result.append('You are recognized as top SoftUni fan!');
  68.                 } else {
  69.                     result.append(`You have ${answerCounter} right answers`);
  70.                 }
  71.             }
  72.  
  73.             button.removeEventListener('click', listen);
  74.         });
  75.     }
  76.  
  77.     function checkOption(option) {
  78.         option.addEventListener('click', function (f) {
  79.            let option = f.target;
  80.  
  81.            option.checked = true;
  82.         });
  83.     }
  84.  
  85.     function disableOptions(firstOption, secondOption, thirdOption) {
  86.         firstOption.disabled = true;
  87.         secondOption.disabled = true;
  88.         thirdOption.disabled = true;
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement