Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  var currentQuestion = 0;
  3. var score = 0;
  4. var totQuestions = questions.length;
  5.  
  6. var container = document.getElementById('quizContainer');
  7. var questionEl = document.getElementById('question');
  8. var opt1 = document.getElementById('opt1');
  9. var opt2 = document.getElementById('opt2');
  10. var opt3 = document.getElementById('opt3');
  11. var opt4 = document.getElementById('opt4');
  12. var nextButton = document.getElementById('nextButton');
  13. var resultCont = document.getElementById('result');
  14. var previousButton = document.getElementById('previousButton')
  15. var restartButton = document.getElementById('restartButton')
  16. var answerButton = document.getElementById('answerButton')
  17.  
  18.  
  19. function loadQuestion (questionIndex) {
  20.     var q = questions[questionIndex];
  21.     questionEl.textContent = (questionIndex + 1) + '. ' + q.question;
  22.     opt1.textContent = q.option1;
  23.     opt2.textContent = q.option2;
  24.     opt3.textContent = q.option3;
  25.     opt4.textContent = q.option4;
  26. };
  27.  
  28.  
  29. function loadpreviousQuestion () {
  30.     var selectedOption = document.querySelector('input[type=radio]:checked');
  31.     if(!selectedOption){
  32.         alert('Please select your answer!');
  33.         return;
  34.     }
  35.     var answer = selectedOption.value;
  36.     /*if(questions[currentQuestion].answer == answer){
  37.         score += 1;
  38.     }*/
  39.     selectedOption.checked = false;
  40.     currentQuestion--;
  41.     if(currentQuestion == totQuestions - 1){
  42.         nextButton.textContent = 'Finish';
  43.     }
  44.     if(currentQuestion == totQuestions){
  45.         container.style.display = 'none';
  46.        
  47.         resultCont.style.display = '';
  48.         resultCont.textContent = 'Your Score: ' + score + ' out of 10';
  49.        
  50.         showAnswers();
  51.        
  52.         return;
  53.     }
  54.  
  55. loadQuestion(currentQuestion);
  56.  
  57. }
  58.  
  59.  
  60.  
  61. function loadNextQuestion () {
  62.     var selectedOption = document.querySelector('input[type=radio]:checked');
  63.     if(!selectedOption){
  64.         alert('Please select your answer!');
  65.         return;
  66.     }
  67.     var answer = selectedOption.value;
  68.     if(questions[currentQuestion].answer == answer){
  69.         score += 1;
  70.     }
  71.     selectedOption.checked = false;
  72.     currentQuestion++;
  73.     if(currentQuestion == totQuestions - 1){
  74.         nextButton.textContent = 'Finish';
  75.     }
  76.     if(currentQuestion == totQuestions){
  77.        
  78.         container.style.display = 'none';
  79.         resultCont.style.display = '';
  80.         resultCont.textContent = 'Your Score: ' + score + ' out of 10';
  81.         answerButton.style.display='block';
  82.         //restartButton.style.display='block';
  83.        
  84.         //showAnswers();
  85.         return;
  86.     }
  87.     loadQuestion(currentQuestion);
  88. }
  89.  
  90. loadQuestion(currentQuestion);
  91.  
  92.  
  93.  
  94.  
  95.  function resetquiz() {
  96.     container.style.display = '';
  97. resultCont.style.display = 'none';
  98. score = 0;
  99. currentQuestion = 0;
  100.  loadQuestion(currentQuestion)
  101.  answerButton.style.display='none';
  102.  nextButton.textContent = 'Next Question';
  103.  
  104.  }
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113. function showAnswers() {
  114.  for(var i = 0; i < questions.length; i++) {
  115.   var info = questions[i];
  116.   var el = document.createElement('div');
  117.   var questionTxt = document.createTextNode((i + 1 ) + '. ' + questions[i].question);
  118.   var answer = ' ' + info['option' + info.answer];
  119.   var answerEl = document.createElement('strong');
  120.   var answerTxt = document.createTextNode(answer);
  121.   answerEl.appendChild(answerTxt);
  122.   el.appendChild(questionTxt);
  123.   el.appendChild(answerEl);
  124.   resultCont.appendChild(el);
  125.  }
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement