Hi,
I picked up this JavaScript code (quiz code) at:
http://javascript.internet.com/misce...oice-quiz.html
and modified it (and may have deleted some line I shouldn't have) so
that it looks like this on my web site:
http://www.goalseek.net/Scripts/qa_XL_History.htm
Can someone explain to me why the external files produce on the .htm
page only partially what they are supposed to produce? The code works
just fine if I have 3 questions in the external file, but as soon as I
add a fourth one or more, the .htm page always shows only 4 questions
with the fourth one truncated with only 2 multiple-choice answers.
The arrays and the loops appear OK to me in the external .js files. So,
what's wrong? Your expertise will mightily be appreciated.
In the .htm file, these tags point to the external .js files in the
same directory as the .htm file:
<script text/javascript src="quiz_funct ions.js"></script>
<script text/javascript src="qa_XL_Hist ory.js"></script>
The qa_XL_History.j s code is this ..... :
=============== =============== =============== =
/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by James Crooke ::
http://javascript.internet.com/misce...oice-quiz.html
Modified by: Marc Bertrand :: http://www.goalseek.net*/
var questions = new Array();
var choices = new Array();
var answers = new Array();
// To add more questions, just follow the format below.
questions[0]="1) Statement number 1";
choices[0]=new Array();
choices[0][0]="Correct answer";
choices[0][1]="Incorrect answer";
choices[0][2]="Incorrect answer";
choices[0][3]="Incorrect answer";
answers[0]=choices[0][0];
questions[1]="2) Statement number 2";
choices[1]=new Array();
choices[1][0]="Incorrect answer";
choices[1][1]="Correct answer";
choices[1][2]="Incorrect answer";
choices[1][3]="Incorrect answer";
answers[1]=choices[1][1];
questions[2]="3) Statement number 3";
choices[2]=new Array();
choices[2][0]="Incorrect answer";
choices[2][1]="Incorrect answer";
choices[2][2]="Correct answer";
choices[2][3]="Incorrect answer";
answers[2]=choices[2][2];
questions[3]="4) Statement number 4";
choices[3]=new Array();
choices[3][0]="Incorrect answer";
choices[3][1]="Incorrect answer";
Choices[3][2]="Correct answer";
Choices[3][3]="Incorrect answer";
answers[3]=choices[3][2];
questions[4]="5) Statement number 5";
choices[4]=new Array();
choices[4][0]="Correct answer";
choices[4][1]="Incorrect answer";
Choices[4][2]="Incorrect answer";
Choices[4][3]="Incorrect answer";
answers[4]=choices[4][0];
questions[5]="6) Statement number 6";
choices[5]=new Array();
choices[5][0]="Incorrect answer";
choices[5][1]="Correct answer";
choices[5][2]="Incorrect answer";
choices[5][3]="Incorrect answer";
answers[5]=choices[5][1];
questions[6]="7) Statement number 7";
choices[6]=new Array();
choices[6][0]="Incorrect answer";
choices[6][1]="Incorrect answer";
choices[6][2]="Correct answer";
choices[6][3]="Incorrect answer";
answers[6]=choices[6][2];
=============== =============== =============== ==========
.... and the quiz_functions. js code is this:
=============== =============== =============== ==========
/* This script and many more are available free online at
The JavaScript Source :: http://javascript.internet.com
Created by James Crooke ::
http://javascript.internet.com/misce...oice-quiz.html
Modified by: Marc Bertrand:: http://www.goalseek.net*/
var useranswers = new Array();
var answered = 0;
function renderQuiz() {
for(i=0;i<quest ions.length;i++ ) {
document.writel n('<p class="question ">' + questions[i] + ' <span
id="result_' + i + '"></span></p>');
for(j=0;j<choic es[i].length;j++) {
document.writel n('<input type="radio" name="answer_' + i + '"
value="' + choices[i][j] + '" id="answer_' + i + '_' + j + '"
class="question _' + i + '" onclick="submit Answer(' + i + ', this,
\'question_' + i + '\', \'label_' + i + '_' + j + '\')" /><label
id="label_' + i + '_' + j + '" for="answer_' + i + '_' + j + '"> ' +
choices[i][j] + '</label><br />');
}
}
document.writel n('<br><br><hr> <br><div id="anID">Clic k the button
<b>Show Score</b> to find your score.</div><p><hr><br> <input
type="submit" value="Show Score"
onclick="WriteS core()"> & nbsp; <inp ut type="submit"
value="Reset Quiz" onclick="resetQ uiz(true)" /></p>');
}
function resetQuiz(showC onfirm) {
//if(showConfirm)
if(!confirm("Ar e you sure you want to reset your answers and start
from the beginning?"))
return false;
document.locati on = document.locati on;
}
function submitAnswer(qu estionId, obj, classId, labelId) {
useranswers[questionId] = obj.value;
answered++;
}
function WriteScore() {
/*if(answered != answers.length) {
alert("You have not answered all of the questions yet!");
return false;
}*/
questionCount = answers.length;
correct = 0;
incorrect = 0;
for(i=0;i<quest ionCount;i++) {
if(useranswers[i] == answers[i])
correct++;
else
incorrect++;
}
pc = Math.round((cor rect / questionCount) * 100);
DynWrite('anID' ,'You scored <b>'+ correct +' correct answers out of '
+ questionCount + '</b> or <b>' + pc + ' %</b>')
}
=============== =============== =============== ==========