Creating random questions and answers quiz

I have a question and answer section of my jQuery mobile 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? 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. There is one question per page, and I want the question to be in the h2 tag and the answers in each div , like below:

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

Another way I was thinking was to create a multipage in jQuery and have 40 pages inside, one for each question, then create a function that randomizes the pages and the user is only shown 6 when pressing next. Is there a better way of doing this than I am proposing?

Hey,
The better way to do this is using php combined with html.
1 - build an array witha all questions
2 - build an other array with all answers
3 - make a random function
4 - make a function to select from each array base on the previews random function.
Hop its help you building this.

1 Like

There’s actually a built in function to do that. :-) Anyway, I fully agree that something like that would better not be hard-coded in the HTML.

Have you considered using a survey library, such as http://surveyjs.org/

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.