Advertisement
Guest User

Untitled

a guest
Sep 28th, 2021
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.     let correct = 0
  3.     let inCorrect = 0
  4.  
  5.     const mapper = {
  6.         'Question #1: Which event occurs when the user clicks on an HTML element?': 'onclick',
  7.         'Question #2: Which function converting JSON to string?': 'JSON.stringify()',
  8.         'Question #3: What is DOM?': 'A programming API for HTML and XML documents'
  9.     }
  10.  
  11.     const questions = document.querySelectorAll('h2');
  12.     let sectionEl = Array.from(document.querySelectorAll('section'));
  13.  
  14.     for (let i = 0; i < questions.length; i++) {
  15.         let currentQuestion = questions[i].textContent
  16.         let buttons = sectionEl[i].querySelectorAll('p');
  17.         for (el of buttons) {
  18.             el.addEventListener('click', clickAnswer)
  19.         }
  20.  
  21.         function clickAnswer(e) {
  22.             if (e.currentTarget.textContent === mapper[currentQuestion]) {
  23.                 correct += 1;
  24.                 if (i < 2) {
  25.                     sectionEl[i].style.display = 'none';
  26.                     sectionEl[i + 1].style.display = 'block';
  27.  
  28.                 }
  29.             } else {
  30.                 if (i < 2) {
  31.                     sectionEl[i].style.display = 'none';
  32.                     sectionEl[i + 1].style.display = 'block';
  33.                 }
  34.             }
  35.  
  36.             if (i === 2) {
  37.                 if (correct === 3) {
  38.                     let result = document.querySelectorAll('.results-inner')[0].children;
  39.                     result[0].textContent = 'You are recognized as top JavaScript fan!';
  40.                     sectionEl[i].style.display = 'none';
  41.                     document.getElementById('results').style.display = 'block';
  42.                 } else {
  43.                     let result = document.querySelectorAll('.results-inner')[0].children;
  44.                     result[0].textContent = `You have ${correct} right answers`;
  45.                     sectionEl[i].style.display = 'none';
  46.                     document.getElementById('results').style.display = 'block';
  47.                 }
  48.             }
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement