Advertisement
George_Ivanov05

quizz_game

Jan 12th, 2022
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const startButton = document.getElementById("start-btn");
  2. const nextButton = document.getElementById("next-btn");
  3. const questionContainerElement = document.getElementById("question-container");
  4. const questionElement = document.getElementById("question");
  5. const answerButtonsElement = document.getElementById("answer-buttons");
  6. const scoreElement = document.getElementById("score");
  7.  
  8. let scoreCnt = 0;
  9.  
  10. let shuffledQuestions, currentQuestionIndex;
  11.  
  12. let correctAnswers = 0;
  13.  
  14. startButton.addEventListener("click", () => {
  15.     startButton.classList.add("hide");
  16.     shuffledQuestions = questions.sort(() => Math.random() - 0.5);
  17.     currentQuestionIndex = 0;
  18.     scoreCnt = 0;
  19.     scoreElement.textContent = "Score: " + 0
  20.     questionContainerElement.classList.remove("hide");
  21.     setNextQuestion();
  22. });
  23. nextButton.addEventListener("click", () => {
  24.     currentQuestionIndex++;
  25.     setNextQuestion();
  26. });
  27.  
  28. // function startGame() {
  29. //     startButton.classList.add("hide");
  30. //     shuffledQuestions = questions.sort(() => Math.random() - 0.5);
  31. //     currentQuestionIndex = 0;
  32. //     scoreCnt = 0;
  33. //     scoreElement.textContent = "Score: " + 0
  34. //     questionContainerElement.classList.remove("hide");
  35. //     setNextQuestion();
  36. // }
  37.  
  38. function setNextQuestion() {
  39.     resetState();
  40.     showQuestion(shuffledQuestions[currentQuestionIndex]);
  41. }
  42.  
  43. function showQuestion(question) {
  44.     questionElement.innerText = question.question;
  45.     question.answers.forEach((answer) => {
  46.         const button = document.createElement("button");
  47.         button.innerText = answer.text;
  48.         button.classList.add("btn");
  49.         if (answer.correct) {
  50.             button.dataset.correct = answer.correct;
  51.         }
  52.         button.addEventListener("click", selectAnswer);
  53.         answerButtonsElement.appendChild(button);
  54.     });
  55. }
  56.  
  57. function resetState() {
  58.     clearStatusClass(document.body);
  59.     nextButton.classList.add("hide");
  60.     while (answerButtonsElement.firstChild) {
  61.         answerButtonsElement.removeChild(answerButtonsElement.firstChild);
  62.     }
  63. }
  64.  
  65. function selectAnswer(e) {
  66.     const selectedButton = e.target;
  67.     const correct = selectedButton.dataset.correct;
  68.     setStatusClass(document.body, correct);
  69.     Array.from(answerButtonsElement.children).forEach((button) => {
  70.         setStatusClass(button, button.dataset.correct);
  71.     });
  72.     if (shuffledQuestions.length > currentQuestionIndex + 1) {
  73.         nextButton.classList.remove("hide");
  74.     } else {
  75.         startButton.innerText = "Restart";
  76.         startButton.classList.remove("hide");
  77.  
  78.     }
  79.  
  80.     if(correct){
  81.        
  82.         let scoreString = scoreElement.textContent;
  83.  
  84.         let currentScore = parseInt(scoreString.slice(7, 8))
  85.  
  86.         currentScore++;
  87.  
  88.         scoreElement.textContent = "Score: " + currentScore;
  89.     }
  90. }
  91.  
  92. function setStatusClass(element, correct) {
  93.     clearStatusClass(element);
  94.     if (correct) {
  95.         element.classList.add("correct");
  96.     } else {
  97.         element.classList.add("wrong");
  98.     }
  99. }
  100.  
  101. function clearStatusClass(element) {
  102.     element.classList.remove("correct");
  103.     element.classList.remove("wrong");
  104. }
  105.  
  106.  
  107. const questions = [{
  108.         question: "До кой век продължава Средновековието?",
  109.         answers: [{
  110.                 text: "до XIII век (включително)",
  111.                 correct: false
  112.             },
  113.             {
  114.                 text: "до XIV век (включително)",
  115.                 correct: true
  116.             },
  117.             {
  118.                 text: "до XV век (включително) ",
  119.                 correct: false
  120.             },
  121.             {
  122.                 text: "до XVI век (включително) ",
  123.                 correct: false
  124.             },
  125.         ],
  126.     },
  127.     {
  128.         question: "С имената на кои библейски герои се свързва мотивът за братоубийството?",
  129.         answers: [{
  130.                 text: "Адам и Ева",
  131.                 correct: false
  132.             },
  133.             {
  134.                 text: "Мойсей и фараона",
  135.                 correct: false
  136.             },
  137.             {
  138.                 text: "Каин и Авел ",
  139.                 correct: true
  140.             },
  141.             {
  142.                 text: "Пилат и Симон ",
  143.                 correct: false
  144.             },
  145.         ],
  146.     },
  147.     {
  148.         question: "Кое НЕ е тема в текстовете на старобългарската литература?",
  149.         answers: [{
  150.                 text: "животът на светците",
  151.                 correct: false
  152.             },
  153.             {
  154.                 text: "славянската азбука ",
  155.                 correct: false
  156.             },
  157.             {
  158.                 text: "реално съществували личности",
  159.                 correct: false
  160.             },
  161.             {
  162.                 text: "природата",
  163.                 correct: true
  164.             },
  165.         ],
  166.     },
  167.     {
  168.         question: "Какво е апология?",
  169.         answers: [{
  170.                 text: "възторжена възхвала на някого или на нещо",
  171.                 correct: true
  172.             },
  173.             {
  174.                 text: "уподобяване на един предмет е друг ",
  175.                 correct: false
  176.             },
  177.             {
  178.                 text: "песен в чест на бог Аполон",
  179.                 correct: false
  180.             },
  181.             {
  182.                 text: "извинение към хората и Бог",
  183.                 correct: false
  184.             },
  185.         ],
  186.     },
  187.     {
  188.         question: `Авторът на "Азбучна молитва" е:`,
  189.         answers: [{
  190.                 text: "създателят на старобългарската кирилска азбука",
  191.                 correct: false
  192.             },
  193.             {
  194.                 text: "най-плодовитият последовател на Солунските братя",
  195.                 correct: true
  196.             },
  197.             {
  198.                 text: `основателят на книжовната школа през "Златния век"`,
  199.                 correct: false
  200.             },
  201.             {
  202.                 text: "най-добрият от преките ученици на Константин-Кирил",
  203.                 correct: false
  204.             },
  205.         ],
  206.     },
  207.     {
  208.         question: `Декамерон"е:`,
  209.        answers: [{
  210.                text: "роман",
  211.                correct: false
  212.            },
  213.            {
  214.                text: "сборник с новели ",
  215.                correct: true
  216.            },
  217.            {
  218.                text: "епическа поема",
  219.                correct: false
  220.            },
  221.            {
  222.                text: "комедия",
  223.                correct: false
  224.            },
  225.        ],
  226.    },
  227.    {
  228.        question: `Кой е основният проблем, поставен в поемата "Илиада"?`,
  229.        answers: [{
  230.                text: "за гибелта на Троя",
  231.                correct: false
  232.            },
  233.            {
  234.                text: "борбата за любовта",
  235.                correct: false
  236.            },
  237.            {
  238.                text: "за войната и победата",
  239.                correct: false
  240.            },
  241.            {
  242.                text: "за наранената чест и достойнство",
  243.                correct: true
  244.            },
  245.        ],
  246.    },
  247. ];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement