View Full Version : Random selection for quiz script???

04-22-2008, 06:26 AM
Hey Everyone!

I am trying to adapt the script found at the following page:


I am trying to find a modification to where instead of having the questions appear in numbered order, to generate in a random order?

Any input would be appreciated!

Philip M
04-22-2008, 09:08 AM
You can easily alter the popsolution.js file as follows:-

var total=5
var question=new Array()
for (i=1;i<=total+1;i++){
j = parseInt(Math.random() * total+1); // 1-5
temp="choice"+j+"=new Array()"

But of course this means that the same question may be repeated.

If you want to allow each question to appear only once you need to keep a record of which questions have already been selected.

"There is hardly anything in the world that some man cannot make a little worse and sell a little cheaper, and the people who consider price only are this man's lawful prey." John Ruskin

04-22-2008, 11:15 PM
You might try adapting something like this:

function Qwiz(){
var q= [
['2', 'Any fool can make a rule, and any fool will mind it.'],
['5', 'Always forgive your enemies, nothing annoys them so much.'],
['4', 'An ignorant person is one who doesn\'t know what you have just found out.'],
['3', 'Beware of the young doctor and the old barber.'],
['1', 'Better to remain silent and be thought a fool than to speak out and remove all doubt.']
this.questions= q.sort(function(a,b){return Math.random()- .5});
this.selectfrom= '1. Abraham Lincoln,2. Henry David Thoreau,3. Benjamin Franklin,4. Will Rogers,5. Oscar Wilde';
Qwiz.prototype.next= function(){
var score= 0, temp= '';

var tem= this.questions.shift();
var A= this.selectfrom.split(',');
var i= A.length;
var q= tem[1];
var a= tem[0];
var s= A.join('\n');
var s2= '\n'+q+'\n\nWho said it? Pick a number.\n\n'+s;
var el= prompt(temp+s2)
if(el.charAt(0)!== a.charAt(0)){
var el= prompt('Try again?\n'+s2);
if(!el || el.charAt(0)!= a.charAt(0)){
i= 0;
if(el.charAt(0)== a.charAt(0)){
score+= i;
temp= ('That is correct! '+score + 'points\n' );
else temp= 'Sorry. Wrong answer.\n';
var final= score>20? (score==25? 'Perfect!\n': 'Excellent!\n'): 'Final score: ';
alert(final +score +' Points')
var Quiz= new Qwiz();