jscheuer1
07-07-2005, 01:41 PM
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:
<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+'\')"> <span id="ans'+i+'" class="chkans"></span><br> ')
write('<br>')
}
}
with (document){
write('<hr><br>')
write('<input type="button" value="See Score" onclick="addup()"> <span id="results"></span><br> <br>')
if (allowDoOvers)
write('<input type="button" value="Start Again" onclick="reset();clearR()">')
write('</form>')
}
</script>
</body></html>
[page deleted without warning by Comcast, a link to a better version is in a following post]
and here is the source:
<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+'\')"> <span id="ans'+i+'" class="chkans"></span><br> ')
write('<br>')
}
}
with (document){
write('<hr><br>')
write('<input type="button" value="See Score" onclick="addup()"> <span id="results"></span><br> <br>')
if (allowDoOvers)
write('<input type="button" value="Start Again" onclick="reset();clearR()">')
write('</form>')
}
</script>
</body></html>