1. Home
  2. Computing & Technology
  3. JavaScript

Random Quiz Maker Addon

Instead of displaying a set sequence of (say 10) questions one after the other in a quiz you might prefer to set up a larger number of questions (say 50) and have the questions to be displayed selected at random from that larger group.

Rather than set up a new quiz maker script to create a single quiz script like that I decided instead to work out code that could be added to several of my quiz makers to add the functionality to the existing quiz makers.

The actual quiz questions selected for inclusion in the quiz will be randomly chosen at the start of the quiz. Information about which entries have been chosen will then be stored in a session cookie so that the selected questions can be displayed in order as the person proceeds through the quiz. If your visitor attempts the quiz a second time during the same browser session then the same questions will be displayed. Session cookies are retained only until the person closes their browser so they will almost certainly get a different random selection of quiz questions on their next visit.

If your visitor does not have first party session cookies enabled then each question will be randomly selected from all possible questions in the quiz and it will be possible for the same question to appear more than once during the same quiz.

The following code can be added to the end of the head script code of any of the quiz scripts where the quiz questions are all displayed by the one page (ie. any of my quiz makers except Quiz Maker 2). The only change that you need to make to the code is to substitute the number of questions you want to include in the randomly generated quiz into the quest variable at the top of the script.

Provided that your quiz array contains at least the specified number of questions then that number of questions will be randomly selected when someone decides to start the quiz. If there are fewer questions then those questions will display in the order entered rather than in a random order.

Explore JavaScript
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.