External .js file execution

M

mb

Hi,

I picked up this JavaScript code (quiz code) at:
http://javascript.internet.com/miscellaneous/multiple-choice-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_functions.js"></script>
<script text/javascript src="qa_XL_History.js"></script>

The qa_XL_History.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/miscellaneous/multiple-choice-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/miscellaneous/multiple-choice-quiz.html
Modified by: Marc Bertrand:: http://www.goalseek.net*/

var useranswers = new Array();
var answered = 0;

function renderQuiz() {
for(i=0;i<questions.length;i++) {
document.writeln('<p class="question">' + questions + ' <span
id="result_' + i + '"></span></p>');
for(j=0;j<choices.length;j++) {
document.writeln('<input type="radio" name="answer_' + i + '"
value="' + choices[j] + '" id="answer_' + i + '_' + j + '"
class="question_' + i + '" onclick="submitAnswer(' + i + ', this,
\'question_' + i + '\', \'label_' + i + '_' + j + '\')" /><label
id="label_' + i + '_' + j + '" for="answer_' + i + '_' + j + '"> ' +
choices[j] + '</label><br />');
}
}
document.writeln('<br><br><hr><br><div id="anID">Click the button
<b>Show Score</b> to find your score.</div><p><hr><br><input
type="submit" value="Show Score"
onclick="WriteScore()">&nbsp;&nbsp;&nbsp;<input type="submit"
value="Reset Quiz" onclick="resetQuiz(true)" /></p>');
}




function resetQuiz(showConfirm) {
//if(showConfirm)
if(!confirm("Are you sure you want to reset your answers and start
from the beginning?"))
return false;
document.location = document.location;
}



function submitAnswer(questionId, 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<questionCount;i++) {
if(useranswers == answers)
correct++;
else
incorrect++;
}
pc = Math.round((correct / questionCount) * 100);
DynWrite('anID','You scored <b>'+ correct +' correct answers out of '
+ questionCount + '</b> or <b>' + pc + ' %</b>')

}

=======================================================
 
L

Lee

(e-mail address removed) said:
Hi,

I picked up this JavaScript code (quiz code) at:
http://javascript.internet.com/miscellaneous/multiple-choice-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.

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];

Javascript is case-sensitive. Typing "Choices" where it is expecting
"choices" is going to break something.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top