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

时间:2014-03-14 18:32:56

标签: javascript

我使用了一个数组,我需要确保在测验中输出数组中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

2 个答案:

答案 0 :(得分:0)

将所有问题存储在一个数组中; shuffle问题;问题数组中的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;
}

答案 1 :(得分:0)

传递问题和您想要的问题数量。返回一组问题。

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;
}