0

I have a question and answer section of my site, where the user is asked 6 questions of out 40 possible questions. There are set answers for each one, but there isn't a correct answer. How do I randomize this in Javascript so that the same questions aren't always asked? I know it can be done with an array for the question and answers but don't know how to randomize them then for each page. This is my html code:

    <h2>What is the weather like today?</h2>

    <div class="answers">

    <div class="answers-left">
        <div class="answer1" tabIndex="1">Sunny</div>
        <div class="answer2" tabIndex="2">Raining</div>
    </div>
    <div class="answers-right">
        <div class="answer3" tabIndex="3">Cloudy</div>
        <div class="answer4" tabIndex="4">Windy</div>
    </div>

        <div class="clear"></div>
        </div>

<div class="next-button">
    <a class="ui-btn" href="question-2.html" rel="external">Next</a>
        </div>

As you can see, the user will select an answer and then click next to go to the next question. I only want one question to show at a time. Any help will be greatly appreciated!

andrea
  • 123
  • 6
  • 13

1 Answers1

1

Step 1) Declare a questions table:

var questions = [1, 2, 3, 4, 5, ..., 40];

Step 2) Get the 1st random question

var random1 = Math.floor(Math.random() * questions.length) ;
var choice1 = questions[random1];

Step 3) Remove it from the table

questions.splice(random1, 1);

Repeat steps 2 & 3 to get more questions

Theodore K.
  • 5,058
  • 4
  • 30
  • 46