Guest User

Untitled

a guest
Dec 13th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const question = document.getElementById("question");
  2. const options = Array.from(document.getElementsByClassName("option-text"));
  3. const scoreText = document.getElementById('score');
  4. const timeText = document.getElementById("finalCountdown");
  5. let secondsLeft = 20;
  6. const scoreNerf = 15;
  7. let currentQuestion = {};
  8. let score = 0;
  9. let availableQuestions = [];
  10. let questions = [
  11.     {
  12.         "question": "Placeholder - Answer is D",
  13.         "option1": "a",
  14.         "option2": "b",
  15.         "option3": "c",
  16.         "option4": "d",
  17.         "answer": 4
  18.     },
  19.     {
  20.         "question": "Placeholder - the answer is B",
  21.         "option1": "a",
  22.         "option2": "b",
  23.         "option3": "c",
  24.         "option4": "d",
  25.         "answer": 2
  26.     },
  27.     {
  28.         "question": "Placeholder - The answer is B",
  29.         "option1": "a",
  30.         "option2": "b",
  31.         "option3": "c",
  32.         "option4": "d",
  33.         "answer": 2
  34.     }
  35. ];
  36.  
  37. startGame = () => {
  38.     availableQuestions = [...questions];
  39.     setTime();
  40.     getNewQuestion();
  41. };
  42.  
  43.  
  44. getNewQuestion = () => {
  45.    
  46.     if (availableQuestions.length === 0) {
  47.        return window.location.assign("end.html");
  48.     }
  49.    
  50.     const questionList = Math.floor(Math.random() * availableQuestions.length);
  51.     currentQuestion = availableQuestions[questionList];
  52.     question.innerText = currentQuestion.question;
  53.  
  54.    
  55.     options.forEach(option => {
  56.         const number = option.dataset["number"];
  57.         option.innerText = currentQuestion["option" + number];
  58.     });
  59.  
  60.    availableQuestions.splice(questionList, 1);
  61.  
  62. };
  63.  
  64.  
  65. options.forEach(option => {
  66.     option.addEventListener("click", e => {
  67.         const selectedOption = e.target;
  68.         const selectedAnswer = selectedOption.dataset["number"];
  69.  
  70.         let classCorrect = 'correct';
  71.         if (selectedAnswer != currentQuestion.answer) {
  72.             classCorrect = 'incorrect';
  73.         };
  74.  
  75.         selectedOption.parentElement.classList.add(classCorrect);
  76.         console.log(classCorrect);
  77.  
  78.         if (classCorrect === 'incorrect') {
  79.             secondsLeft -= scoreNerf;
  80.         }
  81.  
  82.  
  83.         setTimeout(() => {
  84.             selectedOption.parentElement.classList.remove(classCorrect);//this is the removal of the class from the container element.
  85.             getNewQuestion();
  86.         }, 150);
  87.        
  88.     });
  89. });
  90.  
  91. setTime = () => {
  92.     const timerInterval = setInterval(function () {
  93.         secondsLeft--;
  94.         timeText.innerText = secondsLeft;
  95.  
  96.         if (secondsLeft <= 0) {
  97.             secondsLeft = 0;
  98.             return window.location.assign("end.html");
  99.         } else if (availableQuestions.length === "") {
  100.             clearInterval(timerInterval);
  101.             return window.location.assign("end.html");  
  102.      }
  103.      localStorage.setItem("yourScore", secondsLeft);
  104.     }, 1000);
  105.  
  106. };
  107.  
  108. //call function to start quiz.
  109. startGame();
Advertisement
Add Comment
Please, Sign In to add comment