Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-22-2008, 05:26 AM   PM User | #1
booyahboy
New to the CF scene

 
Join Date: Apr 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
booyahboy is an unknown quantity at this point
Random selection for quiz script???

Hey Everyone!

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

http://www.javascriptkit.com/script/...omboquiz.shtml

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!
booyahboy is offline   Reply With Quote
Old 04-22-2008, 08:08 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
You can easily alter the popsolution.js file as follows:-

Code:
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()"
eval(temp);
}
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

Last edited by Philip M; 04-22-2008 at 10:32 AM.. Reason: Add quote of day
Philip M is offline   Reply With Quote
Old 04-22-2008, 10:15 PM   PM User | #3
mrhoo
Regular Coder

 
Join Date: Mar 2006
Posts: 708
Thanks: 30
Thanked 127 Times in 118 Posts
mrhoo will become famous soon enoughmrhoo will become famous soon enough
You might try adapting something like this:

Code:
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= '';
	while(this.questions.length){
	
		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();
Quiz.next()
mrhoo is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:05 AM.


Advertisement
Log in to turn off these ads.