Advertisement
Guest User

Untitled

a guest
Sep 8th, 2023
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve() {
  2.  
  3.   let points = 0;
  4.   //let answers = Array.from(document.querySelectorAll('.answer-text'))
  5.   //let[one, two, three, four, five, six]  = answers
  6.   let currSection = 0;
  7.   let question = document.getElementsByTagName('section');
  8.   let correctAnswers = ["onclick", "JSON.stringify()", "A programming API for HTML and XML documents"]; //add array with the correct answers
  9.  
  10.   Array.from(document.querySelectorAll('.answer-text'))                                                 //add event to answer-text elements instead of quiz-answer
  11.       .map((x) => x.addEventListener('click', (answer) => {
  12.            if (correctAnswers.includes(x.textContent)) {                                                //check if the correct answers array contains the current clicked answer
  13.               points++;
  14.           };
  15.           question[currSection].style.display = 'none';
  16.           currSection++;
  17.  
  18.           currSection !== 3
  19.               ? question[currSection].style.display = 'block'
  20.               : printResult(points);
  21.       }));
  22.   function printResult(points) {
  23.       document.querySelector("#results").style.display = 'block';
  24.       let text = '';
  25.       points === 3
  26.           ? text = 'You are recognized as top JavaScript fan!'
  27.           : text = `You have ${points} right answers`;
  28.       document.querySelector("#results > li > h1").textContent = text;
  29.   }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement