Advertisement
Guest User

Untitled

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