Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $(function() {
  2.    
  3.    
  4.     window.questionIndex = 0;
  5.     window.results = [];
  6.    
  7.  
  8.     if (document.cookie.indexOf("question-index=") >= 0) {
  9.       window.questionIndex = parseInt(Cookies.get('question-index'));
  10.     }
  11.    
  12.     if (document.cookie.indexOf("results") >= 0) {
  13.       window.results = JSON.parse(Cookies.get("results"));
  14.     }
  15.    
  16.  
  17.    
  18.     if ($('#login-form').length > 0) {
  19.        
  20.         /* login form available, must be index.html */
  21.         $('#login-form').submit(function(e) {
  22.             var userName = $('.input-username').val();
  23.             if (userName.length == 0) {
  24.                 alert("Please enter a name longer than 0 characters.");
  25.                 e.preventDefault();
  26.             }
  27.            
  28.             Cookies.set("username", userName);
  29.         });        
  30.        
  31.        
  32.     }
  33.     else {
  34.        
  35.         /* not available, must be question-page.html */
  36.         $.getJSON("questions.json", function(data){
  37.             window.questions = data;
  38.            
  39.             $('.question-number').text(
  40.                 "Question " + (parseInt(window.questionIndex) + 1) +
  41.                 " of " + window.questions.length
  42.             );
  43.            
  44.            
  45.             var question = window.questions[window.questionIndex];
  46.            
  47.             $('.question-image').attr("src", question.image);
  48.            
  49.             $('.question-section > p').text(question.text);
  50.            
  51.             var inputArea = $('.input-area');
  52.            
  53.             inputArea.html("");
  54.            
  55.             for (var i=0; i<question.answers.length; i++) {
  56.                 var answer = question.answers[i];
  57.                
  58.                 inputArea.append(
  59.                     '<button type="button" class="answer-button" onclick="answer(' + i + ');">' +
  60.                     answer.text +
  61.                     '</button>'
  62.                 );            
  63.             }
  64.         });
  65.        
  66.     }
  67.    
  68.    
  69.    
  70.     window.answer = function(answerIndex) {
  71.         var question = window.questions[window.questionIndex];
  72.         var answer = question.answers[answerIndex];
  73.  
  74.         /* save our result onto the array */
  75.         window.results.push(answerIndex);
  76.         Cookies.set('results', window.results);
  77.        
  78.         /* If questionIndex + 1 is higher than the number of questions, got end page. */
  79.         if (window.questionIndex + 1 >= 2) {
  80.             alert("you picked " + answer.text);
  81.             location.href = "results.html";
  82.            
  83.         } else {
  84.             Cookies.set('question-index', window.questionIndex + 1);
  85.             alert("you picked " + answer.text);
  86.             location.reload();        
  87.         }
  88.    
  89.     };
  90.    
  91.    
  92.     window.resetQuestions = function() {
  93.       Cookies.remove("username");
  94.       Cookies.remove('results');
  95.       Cookies.remove('question-index');
  96.       location.href = "index.html";
  97.     };
  98.    
  99.    
  100. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement