nikolayneykov

Untitled

May 20th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve () {
  2.   let rightAnswers = [
  3.     'onclick',
  4.     'JSON.stringify()',
  5.     'A programming API for HTML and XML documents'
  6.   ]
  7.  
  8.   let rightAnswersCounter = 0
  9.  
  10.   Array.from(document.querySelectorAll('.answer-text')).forEach(el =>
  11.     el.addEventListener('click', checkAnswer)
  12.   )
  13.  
  14.   function checkAnswer (el) {
  15.     let currentAnswer = el.target.innerHTML
  16.     currentSection = el.target.parentNode.parentNode.parentNode.parentNode
  17.     nextSection = currentSection.nextElementSibling
  18.  
  19.     rightAnswers.includes(currentAnswer)
  20.       ? rightAnswersCounter++
  21.       : (rightAnswersCounter += 0)
  22.  
  23.     currentSection.style.display = 'none'
  24.     nextSection.style.display = 'block'
  25.  
  26.     if (nextSection.id === 'results') {
  27.       let resultElement = document.querySelector('#results h1')
  28.  
  29.       rightAnswersCounter === 3
  30.         ? (resultElement.innerHTML =
  31.             'You are recognized as top JavaScript fan!')
  32.         : (resultElement.innerHTML = `You have ${rightAnswersCounter} right answers`)
  33.     }
  34.   }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment