0

I'm creating a true/false fact game and its pretty much done, just one thing remaining. The questions always appear in the same order and I'm not sure how to code it, to make the question always appear in a random order. Grateful for help in any way.

Code:

div id="QA">    
    <h2></h2>
    <span id="buttons"></span>
    <p id="mkttext"></p>
    <p><strong>Points</strong> : <span>0</span></p>
    <div id="demo"></div>
    <br>
    <div id="centerbutton">
<button id="restart" onClick="history.go(0)">Restart game</button>
</div>

  </div>


<script>



var questionnaire = [

  {
    "question" : "Q: The earth is round",
    "valid"    : 1, // indicates the correct array number, use 0, 1...
    "buttons"  : ["False", "True"],
    "answers"  : [ "The correct answer is True"]  
  },
  {
    "question" : "Q: The correct answer is True",
    "valid"    : 1,
    "buttons"  : ["False", "True"],
    "answers"  : [ "The correct answer is True"] 
  },
  {
    "question" : "Q: Java = JavaScript",
    "valid"    : 0, 
    "buttons"  : ["False", "True"],
   "answers"  : [ "The correct answer is False"] 
  },
    {
    "question" : "Q: Grapes explode when you put them in the microwave",
    "valid"    : 1,
    "buttons"  : ["False", "True"],
    "answers"  : [ "The correct answer is True"] 
  },
    {
    "question" : "Q: Shakespeare invented both chess and the basketball",
    "valid"    : 0,
    "buttons"  : ["False", "True"],
    "answers"  : [ "The correct answer is False"] 
  },
    {
    "question" : "Q: LoL stands for World of Warcraft",
    "valid"    : 0,
    "buttons"  : ["False", "True"],
    "answers"  : [ "The correct answer is False"] 
  },
    {
    "question" : "Q: The average human will shed 40 pounds of skin in a lifetime.",
    "valid"    : 1,
    "buttons"  : ["False", "True"],
    "answers"  : [ "The correct answer is True"] 
  },
    {
    "question" : "Q: A giraffe can clean its ears with its 121-inch tongue",
    "valid"    : 0,
    "buttons"  : ["False", "True"],
    "answers"  : [ "The correct answer is False"] 
  },
      {
    "question" : "Q:  Nepal is the only country that doesn't have a rectangular flag",
    "valid"    : 1,
    "buttons"  : ["False", "True"],
    "answers"  : [ "The correct answer is True"] 
  },
    {
    "question" : "Q: The match was invented before the cigarette lighter",
    "valid"    : 0,
    "buttons"  : ["False", "True"],
    "answers"  : [ "The correct answer is False"] 
  },
      {
    "question" : "Game over, check your score below.",

  }

];


var $qa       = $('#QA'),
    $question = $("h2", $qa),
    $buttons   = $("#buttons", $qa),
    $points   = $("p>span",$qa),
    questionnaireLength = questionnaire.length,
    qc        = 0, // Current Question counter
    points    = 0; // Current points

function QandA(){

  var quest = questionnaire[qc],
      question = quest.question,
      validIdx = quest.valid,
      btns     = quest.buttons,
      answer   = quest.answers;

  $question.text( question );



  //End of the game
  if(qc >= questionnaireLength -1){ 

    var link = document.getElementById('buttons');
    link.style.display = 'none'; 

  }

  // generate buttons with text:
  $buttons.empty();
  var i=0; i<btns.length;{
    $buttons.append("<button id='btnfalse'>"+ btns[i] +"</button>");
  }
    var i=1; i<btns.length;{
    $buttons.append("<button id='btntrue'>"+ btns[i] +"</button>");
  }



  // Retrieve generated buttons
  var $btn = $("button", $buttons);

  // Assign click
  $btn.one('click', function(){  
    var idx = $btn.index(this); // get button index

    //var parent = document.getElementById('mkttext');
    //var p = document.createElement('p');
    //p.innerHTML = "Game says: "+ answer[idx] ;
    //parent.appendChild(p);

    points += (idx === parseInt(validIdx, 10) ? 1 : -0); 
    $points.text( points );
    // Next question
    qc++; QandA();
  });

}
QandA();

</script>
Umur Kontacı
  • 35,403
  • 8
  • 73
  • 96
  • 4
    possible duplicate of [How to randomize (shuffle) a JavaScript array?](http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array) – JJJ Jan 09 '15 at 21:51
  • Juhana posted the array shuffling function that I like to use. I highly recommend it. – Ding Jan 09 '15 at 21:51
  • Just shuffle your array. Is the order in the array important? – Karl-André Gagnon Jan 09 '15 at 21:51

1 Answers1

0

Add this function

function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex ;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

And then run your array through it like

questionnaire = shuffle(questionnaire);

See http://jsfiddle.net/apcdm3rk/

Ding
  • 3,065
  • 1
  • 16
  • 27