Javascript 如何随机化测验数组,使其仅输出6个问题中的3个

Javascript 如何随机化测验数组,使其仅输出6个问题中的3个,javascript,Javascript,我使用了一个数组,我需要确保数组中6个问题中的3个在测验中输出,并且每次的问题都不同。我该怎么做呢? 以下是我目前的代码: <script langauge="JavaScript"> // number of quiz questions var totalQuestions = 6; // storing answers and user answers var answers = new Array; var userAnswe

我使用了一个数组,我需要确保数组中6个问题中的3个在测验中输出,并且每次的问题都不同。我该怎么做呢? 以下是我目前的代码:

    <script langauge="JavaScript">
    // number of quiz questions
    var totalQuestions = 6;

    // storing answers and user answers
    var answers = new Array;
    var userAnswers = new Array;

    // quiz answers
    answers[1] = "B";
    answers[2] = "C";
    answers[3] = "C";
    answers[4] = "D";
    answers[5] = "B";
answers[6] = "A";



    function SetAnswer(questionNumber, answerSelection) {
        userAnswers[questionNumber] = answerSelection;
    }

    // incorrect answers to questions.
    function MarkWrongQuestions() {
        for(i = 1; i <= totalQuestions; i++) {
            if(answers[i] != userAnswers[i]) {
                document.getElementById(i).className += " wrong";
            }
            }
            }

    // counts and returns number of right answers
    function GetScore() {
        var score = 0;
        for(i = 1; i <= totalQuestions; i++) {
            if(userAnswers[i] == answers[i])
                score++;
        }
        return score;
    }

    // sets classes for each question div to its default styling.
    function ApplyDefaultQuestionStyles() {
        for(i = 1; i <= totalQuestions; i++) {
            if(i % 2 == 0) {
                document.getElementById(i).className = "question";
            }
            else {
                document.getElementById(i).className = "question odd";
            }
        }
         }

    // calls all appropriate functions in order to check answers and mark
    // incorrect questions.
    function CheckQuiz() {
        ApplyDefaultQuestionStyles();
        var totalQuestions = '6';
        var score = GetScore();
        MarkWrongQuestions();
        alert("Your Total Score Is: " + score + " out of " + totalQuestions + ".");

         }

        function result(score,totalQuestions){
          document.write("Score" +score);

        }

        thanks in advance

//问答题数量
var=6;
//存储答案和用户答案
var answers=新数组;
var userAnswers=新数组;
//测验答案
答案[1]=“B”;
答案[2]=“C”;
答案[3]=“C”;
答案[4]=“D”;
答案[5]=“B”;
答案[6]=“A”;
功能设置答案(问题编号、答案选择){
用户回答[问题编号]=回答选择;
}
//对问题的回答不正确。
函数标记问题(){

对于(i=1;i将所有问题存储在一个数组中;问题;
pop()
项从问题数组中删除

questions = ["What's your name?", "What's your surname?", "What's your age?", "What's your job?", "Where do you live?", "When's your birthday?"];
questions = shuffle(questions);
question = questions.pop();
function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;
  while (0 !== currentIndex) {
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }
  return array;
}

传递问题和所需的问题数。返回问题数组

function pickQuestions(questions,num) {
    // can't pick too many
    if(num > questions.length)
        { num = questions.length; }

    // copy questions so that they're not altered
    var avail = questions.slice(0);

    // selected questions
    var q = [];

    // until you've picked all you need
    while(q.length < num) {
        // pick a random question of those available
        var sel = Math.floor(Math.random()*avail.length);

        // add to the selected questions and remove from the list
        q[q.length] = avail.splice(sel,1)[0];
    }

    return q;
}
函数选取问题(问题,数量){
//不能挑太多
if(num>questions.length)
{num=questions.length;}
//复制问题,使其不被修改
var avail=问题。切片(0);
//选定的问题
var q=[];
//直到你选择了所有你需要的
while(q.length
我可以直接进入控制台,键入
userAnswers=answers;
哦,我刚刚正确地回答了所有问题!;)你说的“每次都不同”是什么意思?6个问题中只有3个问题的20种可能组合(如果将相同问题的重新排序计算为不同,则为120个),因此最终您将不得不重复。请检查。我希望这不是针对严肃的web应用程序。您应该使用服务器端应用程序来存储问题和答案,并检查答案的正确性。