By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,708 Members | 955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,708 IT Pros & Developers. It's quick & easy.

External .js file execution

P: n/a

I picked up this JavaScript code (quiz code) at:

and modified it (and may have deleted some line I shouldn't have) so
that it looks like this on my web site:

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 ::
Created by James Crooke ::
Modified by: Marc Bertrand ::*/

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

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

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

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

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

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

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";
================================================== =====

.... and the quiz_functions.js code is this:

================================================== =====

/* This script and many more are available free online at
The JavaScript Source ::
Created by James Crooke ::
Modified by: Marc Bertrand::*/

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

function renderQuiz() {
for(i=0;i<questions.length;i++) {
document.writeln('<p class="question">' + questions[i] + ' <span
id="result_' + i + '"></span></p>');
for(j=0;j<choices[i].length;j++) {
document.writeln('<input type="radio" name="answer_' + i + '"
value="' + choices[i][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[i][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(!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;

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[i] == answers[i])
pc = Math.round((correct / questionCount) * 100);
DynWrite('anID','You scored <b>'+ correct +' correct answers out of '
+ questionCount + '</b> or <b>' + pc + ' %</b>')


================================================== =====

Jan 16 '06 #1
Share this Question
Share on Google+
1 Reply

P: n/a
Yes indeed. Thanks Lee.

Marc Bertrand

Jan 16 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.