...

View Full Version : Randomizing Quiz question & code needed



kenster18
12-14-2010, 07:42 PM
Hey folks,

Forgive me in advance if this is not the correct place to post this thread. I am a new member to this forum and need a little assistance.
I have some script to create a quiz but need a little more detail. Does anyone know some code I can insert, and where to randomize the questions. Secondly if I have a test bank of 100 questions but want to limit the test to 20 questions what code would I need to insert that as well.
Any assistance is very much appreciated. thank you.

kenster

Old Pedant
12-14-2010, 08:07 PM
Example of picking



<script type="text/javascript">
function Question( q, a1, a2, a3, a4 )
{
this.query = q;
this.answers = [ a1, a2, a3, a4 ];
}
var questions = [
new Question( "How many states are there?", 48, 49, 50, 51 ),
new Question( "What is the capital of Idaho?","Idaho Falls","Boise","Pocatello","$1.98"),
new Question( "What state is the Grand Canyon in?", "CA", "AZ", "CO", "Confusion"),
... etc. ...
];

// randomly order the questions...
questions.sort( new function() { return Math.random() - 0.5; } );

// pick the first 5 of the randomized questions:
for ( var qnum = 0; qnum < 5; ++qnum )
{
var q = questions[q];
document.write( '<div class="question">[' + (q+1) + '] '
+ q.query
+ "<ul><li>" + q.answers.join("</li><li>") + "</li></ul></div>" );
}

That's just a starter. It doesn't provide a way for picking the answer(s) [presumably you'd use radio buttons?], but it shows the basics.

kenster18
12-14-2010, 09:12 PM
thanks for the quick reply, much obliged.

jmrker
12-15-2010, 02:34 AM
Minor corrections to see something:


<script type="text/javascript">
function Question( q, a1, a2, a3, a4 )
{
this.query = q;
this.answers = [ a1, a2, a3, a4 ];
}
var questions = [
new Question( "How many states are there?", 48, 49, 50, 51 ),
new Question( "What is the capital of Idaho?","Idaho Falls","Boise","Pocatello","$1.98"),
new Question( "What state is the Grand Canyon in?", "CA", "AZ", "CO", "Confusion")
/// ... etc. ... with NO COMMA after last entry
];

// randomly order the questions...
questions.sort( function() { return Math.random() - 0.5; } );

// display the randomized questions:
for ( var qnum = 0; qnum < questions.length; ++qnum )
{
var q = questions[qnum];
document.write( '<div class="question">[' + (qnum+1) + '] ' + q.query
+ "<ul><li>" + q.answers.join("</li><li>") + "</li></ul></div>" );
}
</script>

Old Pedant
12-15-2010, 02:38 AM
DOH! Thanks.

kenster18
12-20-2010, 01:45 AM
Hey there,

Thank you for the code, I know it will be helpful. Just a little more assistance if you don't mind: I already have the questions written and functioning. The code you supplied me with, where do I place it? Thanks in advance, again, for your help.

Kenster

jmrker
12-20-2010, 01:59 AM
Thought it was fairly well labeled:

Change this area ...


var questions = [
new Question( "How many states are there?", 48, 49, 50, 51 ),
new Question( "What is the capital of Idaho?","Idaho Falls","Boise","Pocatello","$1.98"),
new Question( "What state is the Grand Canyon in?", "CA", "AZ", "CO", "Confusion")
/// ... etc. ... with NO COMMA after last entry
];

kenster18
12-20-2010, 06:46 PM
Thanks for the help. Yes it was fairly well labeled, I just needed a little further guidance. thank you and all the best.

kenster

jmrker
12-20-2010, 06:54 PM
Thanks for the help. Yes it was fairly well labeled, I just needed a little further guidance. thank you and all the best.

kenster

No problem ... post back if you do though. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum