Advertisement
orksnork

Untitled

Jul 24th, 2014
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // take the an array and shuffle it
  2.  
  3. function shuffleArray(array) {
  4.     for (var i = array.length - 1; i > 0; i--) {
  5.         var j = Math.floor(Math.random() * (i + 1));
  6.         var temp = array[i];
  7.         array[i] = array[j];
  8.         array[j] = temp;
  9.     }
  10.     return array;
  11. }
  12.  
  13. // push nextQuestionIndex up
  14.  
  15. function nextQuestion() {
  16.         nextQuestionIndex++;
  17.         if (nextQuestionIndex >= questions.length) {
  18.             nextQuestionIndex = 0;
  19.             pullQuestion();
  20.         }
  21.         else {
  22.         question = questions[nextQuestionIndex];
  23.         }
  24.     }
  25.  
  26. //pull feed, shuffle data, write to question, set question to index 0 of shuffled data
  27.  
  28. function pullQuestion(){
  29.             $.ajax({
  30.                 url: "/gary/feed/",
  31.                 async: false,
  32.                 dataType: 'json',
  33.                 success: function(data) {
  34.                      shuffleArray(data); // housekeeping task
  35.                      data.splice(0, 1);
  36.                      questions = data;
  37.                      question = questions[nextQuestionIndex];
  38.                         }});
  39.                     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement