0

i have used an array and i need to make sure that 3 questions out of 6 in the array are outputted in the quiz and that the questions are different each time. how do i go about doing this? here is my code so far:

    <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
Lanorkin
  • 7,310
  • 2
  • 42
  • 60
  • 1
    I could just go into the console, type `userAnswers = answers;` and ohey, I just answered all the questions correctly! ;) – Niet the Dark Absol Mar 14 '14 at 18:34
  • What do you mean by "different each time"? There are only 20 possible combinations of 3 out of 6 questions (120 if you count reorderings of the same questions as different), so eventually you will _have_ to repeat. Check out [this thread](http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array). – Ted Hopp Mar 14 '14 at 18:36
  • I hope this is not for a serious web application. You should use a server-side application for storing questions and answers and for checking the correctness of an answer. – Giulio Muscarello Mar 14 '14 at 18:45

2 Answers2

0

Store all the questions in an array; shuffle the questions; pop() items out of the questions array.

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;
}
Giulio Muscarello
  • 1,312
  • 2
  • 12
  • 33
0

Pass the questions and the number of questions you want. Returns an array of questions.

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;
}
Dissident Rage
  • 2,610
  • 1
  • 27
  • 33