2016-10-17 14 views
0
var quiz = [ 
     { 
      "question"  : "Q1: Who is the best scientist?", 
      "choices"  : [ 
            "Sir Isaac Newton", 
            "Albert Einstein", 
            "Nicolaus Copernicus", 
            "Ralph Waldo Emmerson" 
           ], 
      "correct"  : "Albert Einstein", 
      "explanation" : "Albert Einstein drafted the special theory of relativity.", 

     }, 
     { 
      "question"  : "Q2: Who looks better?", 
      "choices"  : [ 
            "Thomas", 
            "Dwight Howard", 
            "Benjamin Parker", 
            "Jeremy Lincoln" 
           ], 
      "correct"  : "Benjamin Parker", 
      "explanation" : "He has better features to start with", 
     }, 
     { 
      "question"  : "Q3: What event began on December 25, 1990?", 
      "choices"  : [ 
            "Christmas", 
            "Chinese New Year", 
            "American Civil War began", 
            "Declaration of Independence" 
           ], 
      "correct"  : "Christmas", 
      "explanation" : "Duh, Christmas? I think this needs no future explaination", 
     }, 

    ]; 

function loadQuestion(){ 

     //set temporary variable for creating radio buttons 
     var radioButton; 

     //clear out radio buttons from previous question 
     document.getElementById('content').innerHTML = ""; 

     //loop through choices, and create radio buttons 
     for(var i=0; i < quiz[currentQuestion]["choices"].length; i++){ 

      radioButton = document.createElement('input'); 
      radioButton.type = 'radio'; 
      radioButton.name = 'quiz'; 
      radioButton.id = 'choice'+ (i+1); 
      radioButton.value = quiz[currentQuestion]["choices"][i]; 

      //create label tag, which hold the actual text of the choices 
      var label = document.createElement('label'); 
      label.setAttribute('for','choice'+ (i+1)); 
      label.innerHTML = quiz[currentQuestion]["choices"][i]; 

      //create a <br> tag to separate options 
      var br = document.createElement('br'); 

      //attach them to content. Attach br tag, then label, then radio button 
      document.getElementById('content').insertBefore(br); 
      document.getElementById('content').insertBefore(label, br); 
      document.getElementById('content').insertBefore(radioButton, label); 
     } 
+0

你只是想以隨機順序的問題? – Weedoze

+0

[從JSON中選擇隨機對象]的可能重複(http://stackoverflow.com/questions/30061969/select-random-object-from-json) –

+0

請分享html – brk

回答

0

創建隨機數發生器是這樣的:

var i = quiz.length; var n = Math.floor((Math.random() * i)); var question = quiz[n]

源:
Javascript random
Array.length

0
var currentQuestion = Math.floor(Math.random() * myArray.length); 
0

這裏是所有的quiz項目一個循環,並使用「How to randomize (shuffle) a JavaScript array」的解決方案,以隨機的選擇的排列:

var quiz = [{"question": "Q1: Who is the best scientist?","choices" : ["Sir Isaac Newton","Albert Einstein","Nicolaus Copernicus","Ralph Waldo Emmerson"],"correct" : "Albert Einstein","explanation" : "Albert Einstein drafted the special theory of relativity.",},{"question": "Q2: Who looks better?","choices" : ["Thomas","Dwight Howard","Benjamin Parker","Jeremy Lincoln"],"correct" : "Benjamin Parker","explanation" : "He has better features to start with",},{"question": "Q3: What event began on December 25, 1990?","choices" : ["Christmas","Chinese NewYear","American Civil War began","Declaration of Independence"],"correct" : "Christmas","explanation" : "Duh, Christmas? I think this needs no future explaination"}], 
 
    shuffleArray = function (array) { 
 
     for (var i = array.length - 1; i > 0; i--) { 
 
     var j = Math.floor(Math.random() * (i + 1)), 
 
      temp = array[i]; 
 
     array[i] = array[j]; 
 
     array[j] = temp; 
 
     } 
 
     return array; 
 
    }, 
 
    loadQuestions = function (quiz) { 
 
     quiz.forEach(function(item) { 
 
     var choices = shuffleArray(item.choices); 
 
     console.log(item.question); 
 
     choices.forEach(function(choice) { 
 
      console.log('-', choice); 
 
     }); 
 
     }); 
 
    } 
 
; 
 

 
// Load questions 
 
loadQuestions(quiz);