Advertisement
kstoyanov

08. *JavaScript Quizz

Sep 29th, 2020
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.   const rightAnswers = ['onclick', 'JSON.stringify()', 'A programming API for HTML and XML documents'];
  3.  
  4.   const possibleAnswers = document.getElementsByClassName('answer-wrap');
  5.  
  6.   Array.from(possibleAnswers).forEach((answer) => answer.addEventListener('click', selected));
  7.  
  8.   let indexSection = 0;
  9.   let validAnswers = 0;
  10.   const allSections = document.getElementsByTagName('section');
  11.   function selected() {
  12.     if (rightAnswers.includes(this.children[0].textContent)) {
  13.       validAnswers++;
  14.     }
  15.     allSections[indexSection].style.display = 'none';
  16.     if (indexSection < allSections.length - 1) {
  17.       allSections[indexSection + 1].style.display = 'block';
  18.     } else {
  19.       document.getElementById('results').style.display = 'block';
  20.       if (validAnswers == 3) {
  21.         document.getElementById('results').children[0].children[0].textContent = 'You are recognized as top JavaScript fan!';
  22.       } else {
  23.         document.getElementById('results').children[0].children[0].textContent = `You have ${validAnswers} right answers`;
  24.       }
  25.     }
  26.     indexSection++;
  27.   }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement