Results 1 to 3 of 3

Thread: Quiz Script

  1. #1
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default Quiz Script

    I was moonlighting a little over at Coding Forums and ran across a thread where someone wanted a script for a quiz. Actually it was two different threads and two different script quiz ideas. I really got into the second one and would like folks to have a look. It is an array driven script that writes out the quiz for you and will score the answers. It has optional peeking at answers, optional do overs and accommodates any number of questions, each with any number of possible answers. I'm kind of hoping it can get featured here on DD so I can change my signature but, not to worry, I have a number of other efforts in the works. Here is a link to it:

    [page deleted without warning by Comcast, a link to a better version is in a following post]

    and here is the source:

    Code:
    <html>
    <head>
    <title>State Capitols Quiz</title>
    
    <style type="text/css">
    .chkans {
    margin:10px 10px -5px 0;
    } 
    
    span.chkans {
    position:relative;
    top:5px
    } 
    
    * html span.chkans {
    top:2px
    } 
    </style>
    
    </head>
    <body>
    <h1>State Capitols Quiz</h1>
    This page will tell what you know about state capitols.<br>
    <h2>Directions:</h2>
    Click on the correct answer.<br><br>
    
    <script type="text/javascript">
    /*/////////////////////////////
    * Quiz Writing & Scoring Script
    * © 2005 John Davenport Scheuer
    * jscheuer1TAKEOUTCAPSFOREMAIL@comcast.net
    * permission to use granted
    * this credit must stay intact
    /////////////////////////////*/
    
    //Set to 0 for no answers given away
    //Set to 1 to allow peeking
    var allowPeeking=1
    
    //Set to 0 for no Starting Over without refresh
    //Set to 1 to allow Easy Do Overs
    var allowDoOvers=1
    
    questions=new Array();
    //Set Questions and Answers Arrays Below.
    //Questions come first.  Then come the answer pairs:
    //Follow the right answer with "right", all wrong ones with ""
    questions[0]=["What is the capitol of Pennsylvania?", "Harrisburg", "right", "Oswego", "", "Philadelphia", "", "Denver", ""]
    questions[1]=["What is the capitol of New Jersey?", "Princeton", "", "North Orange", "", "Trenton", "right", "Labrador", ""]
    questions[2]=["What is the capitol of Montana?", "Billings", "", "Helena", "right", "Shelby", "", "Tabasco", ""]
    questions[3]=["What is the capitol of New York?", "Syracuse", "", "New York City", "", "Buffalo", "", "Albany", "right"]
    questions[4]=["What is the capitol of Kansas?", "Atchison", "", "Topeka", "right", "Dodge City", "", "Manhattan", ""]
    questions[5]=["Are we having fun yet?", "Yes", "right", "No", ""]
    
    ////////////////Stop Editing///////////////
    
    for (i = 0; i < questions.length; i++){
    for (j = 0; j < questions[i].length; j++){
    if (questions[i][j]=="")
    questions[i][j]=("w"+i)+j
    if (questions[i][j]=="right")
    questions[i][j]="right"+i
    }
    }
    var ie=document.all
    function showAnswer(el,ans){
    ie? ie[el].innerHTML='The answer is: '+ ans : document.getElementById(el).innerHTML='The answer is: '+ ans
    }
    
    function addup()  {
    var q, right, statement, total=0
    quizQuests=new Array();
    for (i = 0; i < questions.length; i++)
    quizQuests[i]=0
    if (document.forms.quiz.q0['right0']){
    for (i = 0; i < questions.length; i++){
    q="q"+i
    right="right"+i
    if (document.forms.quiz[q][right].checked)
    quizQuests[i]=1
    }
    }
    else if (document.getElementById){
    for (i = 0; i < questions.length; i++){
    right="right"+i
    if (document.getElementById(right).checked)
    quizQuests[i]=1
    }
    }
    else
    return;
    for (i = 0; i < questions.length; i++)
    total += quizQuests[i]
    /*/////////////////////////////////////////////////
    Set score response below.
    ///////////////////Edit Below to Suit/////////////*/
    statement='You scored '+ total +' out of '+ questions.length +' correct, '+ Math.round(total/questions.length*100) +'%'
    /////////////////Stop Editing///////////////////
    ie? ie.results.innerHTML=statement : document.getElementById('results').innerHTML=statement
    }
    function clearR(){
    ie? ie.results.innerHTML='' : document.getElementById('results').innerHTML=''
    for (i = 0; i < questions.length; i++)
    if (allowPeeking)
    ie? ie["ans"+i].innerHTML='' : document.getElementById("ans"+i).innerHTML=''
    window.scrollTo(0,0);
    }
    document.write('<hr><form name="quiz">')
    var correct, answersString
    for (i = 0; i < questions.length; i++){
    answersString=''
    for (k = 1; k < questions[i].length; k+=2)
    answersString+='<input id="'+questions[i][(k+1)]+'" type="radio" unchecked name="q'+i+'"><label for="'+questions[i][(k+1)]+'">'+questions[i][k]+'</label><br>'
    for (j = 0; j < questions[i].length; j++){
    if (questions[i][j]=="right"+i)
    correct=questions[i][j-1]
    }
    with (document){
    write('Question '+(i+1)+':<br>')
    write(questions[i][0]+'<br>')
    write(answersString)
    if (allowPeeking)
    write('<input class="chkans" type="button" value="Check Answer" onclick="showAnswer(\'ans'+i+'\',\''+correct+'\')">&nbsp;<span id="ans'+i+'" class="chkans"></span><br>&nbsp;')
    write('<br>')
    }
    }
    with (document){
    write('<hr><br>')
    write('<input type="button" value="See Score" onclick="addup()">&nbsp;&nbsp;<span id="results"></span><br>&nbsp;<br>')
    if (allowDoOvers)
    write('<input type="button" value="Start Again" onclick="reset();clearR()">')
    write('</form>')
    }
    </script>
    
    </body></html>
    Last edited by jscheuer1; 05-06-2016 at 04:52 AM. Reason: remove broken link
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  2. #2
    Join Date
    May 2016
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Why quiz question is not bold or clear help.

    sir
    First i appreciate your work.Thank you.I have created quiz with your code.When i create question with option.it is shown in browser hazzy not clear.How i make question and its option in colur or bold fold.
    Please help.


    i used this code

    Code:
    <html>
    <head>
    <title>State Capitols Quiz</title>
    
    <style type="text/css">
    .chkans {
    margin:10px 10px -5px 0;
    } 
    
    span.chkans {
    position:relative;
    top:5px
    } 
    
    * html span.chkans {
    top:2px
    } 
    </style>
    
    </head>
    <body>
    <h1>State Capitols Quiz</h1>
    This page will tell what you know about state capitols.<br>
    <h2>Directions:</h2>
    Click on the correct answer.<br><br>
    
    <script type="text/javascript">
    /*/////////////////////////////
    * Quiz Writing & Scoring Script
    * © 2005 John Davenport Scheuer
    * jscheuer1TAKEOUTCAPSFOREMAIL@comcast.net
    * permission to use granted
    * this credit must stay intact
    /////////////////////////////*/
    
    //Set to 0 for no answers given away
    //Set to 1 to allow peeking
    var allowPeeking=1
    
    //Set to 0 for no Starting Over without refresh
    //Set to 1 to allow Easy Do Overs
    var allowDoOvers=1
    
    questions=new Array();
    //Set Questions and Answers Arrays Below.
    //Questions come first.  Then come the answer pairs:
    //Follow the right answer with "right", all wrong ones with ""
    questions[0]=["What is the capitol of Pennsylvania?", "Harrisburg", "right", "Oswego", "", "Philadelphia", "", "Denver", ""]
    questions[1]=["What is the capitol of New Jersey?", "Princeton", "", "North Orange", "", "Trenton", "right", "Labrador", ""]
    questions[2]=["What is the capitol of Montana?", "Billings", "", "Helena", "right", "Shelby", "", "Tabasco", ""]
    questions[3]=["What is the capitol of New York?", "Syracuse", "", "New York City", "", "Buffalo", "", "Albany", "right"]
    questions[4]=["What is the capitol of Kansas?", "Atchison", "", "Topeka", "right", "Dodge City", "", "Manhattan", ""]
    questions[5]=["Are we having fun yet?", "Yes", "right", "No", ""]
    
    ////////////////Stop Editing///////////////
    
    for (i = 0; i < questions.length; i++){
    for (j = 0; j < questions[i].length; j++){
    if (questions[i][j]=="")
    questions[i][j]=("w"+i)+j
    if (questions[i][j]=="right")
    questions[i][j]="right"+i
    }
    }
    var ie=document.all
    function showAnswer(el,ans){
    ie? ie[el].innerHTML='The answer is: '+ ans : document.getElementById(el).innerHTML='The answer is: '+ ans
    }
    
    function addup()  {
    var q, right, statement, total=0
    quizQuests=new Array();
    for (i = 0; i < questions.length; i++)
    quizQuests[i]=0
    if (document.forms.quiz.q0['right0']){
    for (i = 0; i < questions.length; i++){
    q="q"+i
    right="right"+i
    if (document.forms.quiz[q][right].checked)
    quizQuests[i]=1
    }
    }
    else if (document.getElementById){
    for (i = 0; i < questions.length; i++){
    right="right"+i
    if (document.getElementById(right).checked)
    quizQuests[i]=1
    }
    }
    else
    return;
    for (i = 0; i < questions.length; i++)
    total += quizQuests[i]
    /*/////////////////////////////////////////////////
    Set score response below.
    ///////////////////Edit Below to Suit/////////////*/
    statement='You scored '+ total +' out of '+ questions.length +' correct, '+ Math.round(total/questions.length*100) +'%'
    /////////////////Stop Editing///////////////////
    ie? ie.results.innerHTML=statement : document.getElementById('results').innerHTML=statement
    }
    function clearR(){
    ie? ie.results.innerHTML='' : document.getElementById('results').innerHTML=''
    for (i = 0; i < questions.length; i++)
    if (allowPeeking)
    ie? ie["ans"+i].innerHTML='' : document.getElementById("ans"+i).innerHTML=''
    window.scrollTo(0,0);
    }
    document.write('<hr><form name="quiz">')
    var correct, answersString
    for (i = 0; i < questions.length; i++){
    answersString=''
    for (k = 1; k < questions[i].length; k+=2)
    answersString+='<input id="'+questions[i][(k+1)]+'" type="radio" unchecked name="q'+i+'"><label for="'+questions[i][(k+1)]+'">'+questions[i][k]+'</label><br>'
    for (j = 0; j < questions[i].length; j++){
    if (questions[i][j]=="right"+i)
    correct=questions[i][j-1]
    }
    with (document){
    write('Question '+(i+1)+':<br>')
    write(questions[i][0]+'<br>')
    write(answersString)
    if (allowPeeking)
    write('<input class="chkans" type="button" value="Check Answer" onclick="showAnswer(\'ans'+i+'\',\''+correct+'\')">&nbsp;<span id="ans'+i+'" class="chkans"></span><br>&nbsp;')
    write('<br>')
    }
    }
    with (document){
    write('<hr><br>')
    write('<input type="button" value="See Score" onclick="addup()">&nbsp;&nbsp;<span id="results"></span><br>&nbsp;<br>')
    if (allowDoOvers)
    write('<input type="button" value="Start Again" onclick="reset();clearR()">')
    write('</form>')
    }
    </script>
    
    </body></html>
    Last edited by jscheuer1; 05-06-2016 at 03:28 AM. Reason: delete stray quote tag

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I'm not really sure what you mean. I copied your code, looks clear to me. Not bold though. You should be able to just use bold tags (<b></b>) or set the font-weight to bold in css style, as well as font-type, etc. By the way, since my previous link is broken, and there appears to be a more recent version (v10 as opposed to v5) of this script on my computer, I've just uploaded it to my new server:

    http://jscheuer1.com/quiz/

    Perhaps that version will be of help. If you want more help, please clarify.
    Last edited by jscheuer1; 05-06-2016 at 03:32 AM.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

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