Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Randomizing Quiz question & code needed

1. ## Randomizing Quiz question & code needed

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

• Example of picking

Code:
```<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.

• ## Thanks

thanks for the quick reply, much obliged.

• Minor corrections to see something:
Code:
```<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>```

• DOH! Thanks.

• 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

• Thought it was fairly well labeled:

Change this area ...
Code:
```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
];```

• ## Thanks again

Thanks for the help. Yes it was fairly well labeled, I just needed a little further guidance. thank you and all the best.

kenster

• Originally Posted by kenster18
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.

•