如何在Javascript

时间:2016-03-29 15:49:11

标签: javascript

我正在尝试生成一个测验,当重新加载页面时,将从6的数组中显示三个随机问题。

测验有效,但以相同的顺序显示所有问题,我希望只有3个随机问题

我使用的代码如下:



  var pos = 0, test, test_status, question, choice, choices, chA, chB, chC, correct = 0;

  var questions = [
    ["Question 1", "1", "2", "3", "C"],
    ["Question 2", "X", "Y", "Z", "B"],
    ["Question 3", "4", "5", "6", "C"],
    ["Question 4", "A", "B", "C", "A"],
    ["Question 5", "7", "8", "9", "B"],
    ["Question 6", "M", "N", "O", "A"]
  ];

  function get(x){
    return document. getElementById(x);
  }
  function renderQuestion(){
    test = get("test");
    if(pos >= questions.length){
      test.innerHTML = "You got " +correct+ " of "+questions.length+" questions correct";
      get("test_status").innerHTML = "Test Completed";
      pos = 0;
      correct = 0;
      return false;
    }
    get("test_status").innerHTML = "Question " + (pos+1) + " of " + questions.length;
    question = questions[pos] [0];
    chA = questions[pos][1];
    chB = questions[pos][2];
    chC = questions[pos][3];
    test.innerHTML = "" +question+" <br>";
    test.innerHTML += "<input type='radio' name ='choices' value ='A'> " +chA+"<br>";
    test.innerHTML += "<input type='radio' name ='choices' value ='B'> " +chB+"<br>";
    test.innerHTML += "<input type='radio' name ='choices' value ='C'> " +chC+"<br><br>";
    test.innerHTML += "<button onclick='checkAnswer()' >Submit Answer</button>";
  }
  function checkAnswer(){
    choices = document.getElementsByName('choices');
      for (var i = 0; i < choices.length; i++) {
        if(choices[i].checked){
          choice = choices[i].value;
        }
      }
      if (choice == questions [pos][4]) {
        correct++;
      }
      pos++;
      renderQuestion();
  }
  window.addEventListener("load", renderQuestion, false);
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

&#13;
&#13;
var questions = [
  ["Question 1", "1", "2", "3", "C"],
  ["Question 2", "X", "Y", "Z", "B"],
  ["Question 3", "4", "5", "6", "C"],
  ["Question 4", "A", "B", "C", "A"],
  ["Question 5", "7", "8", "9", "B"],
  ["Question 6", "M", "N", "O", "A"]
];

var display = getRandomValues(questions,3);

print(display);

function getRandomValues(arr, count){
  // Create a copy so you do not modify original array
  var _tmp = arr.slice();
  var result = [];
  
  // Loop based on count
  for(var i=0; i<count; i++){
    
    // Calculate a random number and round it. Take Mod based on array.length
    var random = Math.floor(Math.random() * 10) % _tmp.length;
    
    // Push necessary item in array to be returned.
    result.push(_tmp[random]);
    
    // Remove item that has been added to prevent duplicate entries
    _tmp.splice(random,1);
  }
  return result;
}

function print(arr){
  document.write("<pre>" +JSON.stringify(arr,0,4)+ "</pre>");
}
&#13;
&#13;
&#13;

另外我建议您使用对象数组而不是数组数组

var questions = [
  {title:"Question 1", options:["1", "2", "3", "C"]},
  {title:"Question 2", options:["X", "Y", "Z", "B"]},
  {title:"Question 3", options:["4", "5", "6", "C"]},
  {title:"Question 4", options:["A", "B", "C", "A"]},
  {title:"Question 5", options:["7", "8", "9", "B"]},
  {title:"Question 6", options:["M", "N", "O", "A"]}
];

答案 1 :(得分:0)

也许this可用于生成随机数。您可能希望在0到5之间生成一个,但仍然如此。然后,如果你不想要两次同样的问题,你需要跟踪当然要问的问题。