Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post 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

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,103
    Thanks
    80
    Thanked 4,554 Times in 4,518 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Thanks

    thanks for the quick reply, much obliged.

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,130
    Thanks
    38
    Thanked 504 Times in 498 Posts

    Lightbulb

    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>
    Last edited by jmrker; 12-15-2010 at 03:41 AM.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,103
    Thanks
    80
    Thanked 4,554 Times in 4,518 Posts
    DOH! Thanks.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #7
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,130
    Thanks
    38
    Thanked 504 Times in 498 Posts
    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
       ];

  • #8
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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

  • #9
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,130
    Thanks
    38
    Thanked 504 Times in 498 Posts

    Arrow

    Quote Originally Posted by kenster18 View Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •