Click to See Complete Forum and Search --> : Help with a quiz


jake_604
04-25-2003, 07:48 AM
Hi, i have the following code, and the quetions/answers are stored in an external .js file, but is it possible to add a countdown timer for each question, so that the users have 10 seconds to answer each question.

Thanks for any help, this is the code:


<SCRIPT LANGUAGE="JavaScript">

// Define global variables and arrays
var questionIndex = 0;
var checker = true;
var keeper = new Array();
var performance = new Array('No offense, but you suck.','Few have done worse.','You are JavaScriptually impaired.','You need some real work.','You\'re better than some, but not many.','You are an adequate JavaScripter.','You are a formidable JavaScripter.','You are an excellent JavaScripter.','You are an exhalted JavaScript guru.');
var correctAnswers = 0;
var jsAbility;
var preventer = 1;
var spawn;

// For resetting all variables and arrays for a retake
function itemReset() {
questionIndex = 0;
keeper = new Array();
correctAnswers = 0;
preventer = 1;
jsAbility = null;
}

// Administer the test and record the answers
function startQuiz(openStatus, whetherOrNot) {
checker = openStatus;
if (whetherOrNot < questionIndex) { busted(); return; }
if (checker) {
spawn = open('','','width=550,height=350,scrollbars=yes');
!checker;
}

if (questionIndex == killers.length) { gradeTest(); return; }

// Separate each array element to form the questions and answr options
var question = killers[questionIndex].substring(0, killers[questionIndex].indexOf('$$'));
var optionA = killers[questionIndex].substring(killers[questionIndex].indexOf('$$') + 2, killers[questionIndex].indexOf('%%'));
var optionB = killers[questionIndex].substring(killers[questionIndex].indexOf('%%') + 2, killers[questionIndex].indexOf('^^'));
var optionC = killers[questionIndex].substring(killers[questionIndex].indexOf('^^') + 2, killers[questionIndex].indexOf('&&'));
var optionD = killers[questionIndex].substring(killers[questionIndex].indexOf('&&') + 2, killers[questionIndex].length);

// Write the questons and answrs options to the new window
spawn.document.clear();
spawn.document.open();
spawn.document.writeln('<HTML><HEAD><TITLE>JavaScript Brain Masher</TITLE></HEAD>');
spawn.document.writeln('<BODY BGCOLOR="\WHITE\">');
spawn.document.writeln('<FONT FACE=\"Arial\">');
spawn.document.writeln('<H2>Question ' + (questionIndex + 1) + '</H2>');
spawn.document.writeln('<FORM>');
spawn.document.writeln('<B>' + question + '</B><BR><BR>');
spawn.document.writeln('<INPUT TYPE=RADIO NAME=\"answer\" VALUE=\"a\" onClick="opener.keeper[opener.questionIndex - 1] = this.value; opener.startQuiz(false, ' + preventer + ')\">' + optionA + '<BR>');
spawn.document.writeln('<INPUT TYPE=RADIO NAME=\"answer\" VALUE=\"b\" onClick="opener.keeper[opener.questionIndex - 1] = this.value; opener.startQuiz(false, ' + preventer + ')\">' + optionB + '<BR>');
spawn.document.writeln('<INPUT TYPE=RADIO NAME=\"answer\" VALUE=\"c\" onClick="opener.keeper[opener.questionIndex - 1] = this.value; opener.startQuiz(false, ' + preventer + ')\">' + optionC + '<BR>');
spawn.document.writeln('<INPUT TYPE=RADIO NAME=\"answer\" VALUE=\"d\" onClick="opener.keeper[opener.questionIndex - 1] = this.value; opener.startQuiz(false, ' + preventer + ')\">' + optionD + '<BR>');
spawn.document.writeln('</FORM></BODY></HTML>');
spawn.document.close();

// Increment variables for the next question
questionIndex++;
preventer++;
}

function gradeTest() {
// Increment the variables for the last time
questionIndex++;
preventer++;

// Compare student answers with correct answers
for (var i = 0; i < killers.length; i++) {
if (keeper[i] == answers[i]) {
correctAnswers++;
}
}

// Determine a ranking according to number of corrrect answers
jsAbility = performance[Math.ceil((correctAnswers/killers.length) * performance.length - 1)];

// Print the test results
printResults();
}

// Print the questions, answer options, and other info
function printResults() {
spawn.document.clear();
spawn.document.open();
spawn.document.writeln('<HTML><HEAD>');
spawn.document.writeln('<TITLE>JavaScript Brain Masher Results</TITLE>');
spawn.document.writeln('</HEAD><BODY BGCOLOR=\"WHITE\">');
spawn.document.writeln('<FONT FACE=\"Arial\">');
spawn.document.writeln('<H2>You scored ' + correctAnswers + '/' + killers.length + ' correctly.</H2>');
spawn.document.writeln('<B>Ranking: ' + jsAbility + '</B><BR>');
spawn.document.writeln('<BR><BR><FONT SIZE=\"4\">Here is how you scored: </FONT><BR><BR>');

for (var i = 0; i < killers.length; i++) {
spawn.document.writeln('\n\n<B>Question ' + (i + 1) + '</B><BR>');
spawn.document.writeln(killers[i].substring(0, killers[i].indexOf('$$')));
spawn.document.writeln('<BR><BR>\n<FONT SIZE=\"-1\">a. ' + killers[i].substring((killers[i].indexOf('$$') + 2), killers[i].indexOf('%%')) + '<BR>');
spawn.document.writeln('b. ' + killers[i].substring(killers[i].indexOf('%%') + 2, killers[i].indexOf('^^')) + '<BR>');
spawn.document.writeln('c. ' + killers[i].substring(killers[i].indexOf('^^') + 2, killers[i].indexOf('&&')) + '<BR>');
spawn.document.writeln('d. ' + killers[i].substring(killers[i].indexOf('&&') + 2, killers[i].length) + '<BR></FONT>');

// Determine if the student answered each question appropriately, and display accordingly
if (keeper[i] == answers[i]) {
spawn.document.writeln('<B><I><FONT COLOR=\"GREEN\">You answered this correctly.</FONT></I></B>\n<BR><BR><BR>');
}
else { spawn.document.writeln('<B><I><FONT COLOR=\"RED\">The correct answer is: ' + answers[i] + '\n<BR><BR><BR></FONT></I></B>'); }
}

// Add extra info about the ranking, questions, and application
spawn.document.writeln('\n\n<BIG>Your Ranking</BIG><BR><BR>If you came out on top, congratulations! You have a good grasp of JavaScript syntax and applications. If not, don\'t worry. This test was ');
spawn.document.writeln('designed to be difficult. If it wasn\'t hard, doing well would mean much. You can always <A HREF="javascript: opener.itemReset(); opener.startQuiz(false);" onMouseOver="window.status=\'\'; return true"><B>retake</B></A> it. ');

spawn.document.writeln('\n\n<BR><BR><BR><BIG>Questions About Those Questions?</BIG><BR><BR>Some of those were pretty rough, huh? Some even downright unfair? That\'s open for debate, but one thing is ');
spawn.document.writeln('certain: Each answer is documented somewhere. If you have a comment, compliment, or gripe about one or more of them, e-mail me. We\'ll electronically try to argue each other into submission. ');

spawn.document.writeln('\n\n<BR><BR><BR>\n\n<BIG>A Word About The Application</BIG><BR><BR>This test is pure unadulterated JavaScript. From the administration to the grading to the results display, everything happens through JavaScript. For those of you who want to ');
spawn.document.writeln('<A HREF=\"download/masher.zip\" onMouseOver=\"window.status=\'\'; return true"><B>download</B></A> this application and modify it for a test of your own, you\'ll probably want a different method for grading. Although somewhat of an annoyance, it wouldn\'t be too hard to get the ');
spawn.document.writeln('answers to this test. You might want to send the results to a server for grading.\n<BR><BR><BR>');
spawn.document.writeln('\n\n</BODY></HTML>');
spawn.document.close();
}

function busted() {
alert('Sorry, you only get one shot at each question.');
spawn.history.forward();
spawn.focus();
}

</SCRIPT>



Thank you for any help.

jake_604
04-25-2003, 08:28 AM
Please can somebody help!?

AdamBrill
04-25-2003, 01:03 PM
Could you post your entire page and external files? The script that you posted doesn't do anything by itself, so it would be extremely hard to work on... ;)