[Javascript] wrox code question
carl
carlcullum at home.com
Mon Jul 9 14:29:45 CDT 2001
Sorry if this has been beaten up before, but I am working through the Wrox Beginning Javascript book, and have one thing that I dont understand (well, more than one thing, but only one question here)
the code, from their site, is
-------------------------------------------------
<HTML>
<HEAD>
<TITLE>Wrox Online Trivia Quiz</TITLE>
<SCRIPT LANGUAGE=JavaScript>
var questionNumber;
function answerCorrect(questionNumber, answer)
{
// declare a variable to hold return value
var correct = false;
// if answer provided is same as correct answer then correct variable is true
if (answer == answers[questionNumber])
correct = true;
// return whether the answer was correct (true or false)
return correct;
}
function getQuestion()
{
questionNumber = Math.floor(Math.random() * (questions.length));
var questionHTML = "<P>" + questions[questionNumber][0] + "</P>";
var questionLength = questions[questionNumber].length;
var questionChoice;
for (questionChoice = 1;questionChoice < questionLength;questionChoice++)
{
questionHTML = questionHTML + "<INPUT TYPE=radio NAME=radQuestionChoice"
if (questionChoice == 1)
{
questionHTML = questionHTML + " CHECKED";
}
questionHTML = questionHTML + ">";
questionHTML = questionHTML + questions[questionNumber][questionChoice];
questionHTML = questionHTML + "<BR>";
}
document.QuestionForm.txtQNumber.value = questionNumber + 1;
return questionHTML;
}
function buttonCheckQ_onclick()
{
var answer = 0;
while (document.QuestionForm.radQuestionChoice[answer].checked != true)
{
answer++;
}
answer = String.fromCharCode(65 + answer);
if (answerCorrect(questionNumber,answer) == true)
{
alert("You got it right");
}
else
{
alert("You got it wrong");
}
window.location.reload();
}
</SCRIPT>
</HEAD>
<BODY>
<SCRIPT LANGUAGE=JavaScript>
// Questions variable will holds questions
var questions = new Array();
var answers = new Array();
// define question 1
questions[0] = new Array();
// the question
questions[0][0] = "The Beatles were";
// first choice
questions[0][1] = "A sixties rock group from Liverpool";
// second choice
questions[0][2] = "Four musically gifted insects";
// third choice
questions[0][3] = "I don't know - can I have the questions on Baseball please";
// assign answer for question 1
answers[0] = "A";
// define question 2
questions[1] = new Array();
questions[1][0] = "Homer Simpson's favorite food is";
questions[1][1] = "Fresh salad";
questions[1][2] = "Doughnuts";
questions[1][3] = "Bread and water";
questions[1][4] = "Apples";
// assign answer for question 2
answers[1] = "B";
// define question 3
questions[2] = new Array();
questions[2][0] = "Lisa Simpson plays which musical instrument";
questions[2][1] = "Clarinet";
questions[2][2] = "Oboe";
questions[2][3] = "Saxophone";
questions[2][4] = "Tubular Bells";
// assign answer for question 3
answers[2] = "C";
</SCRIPT>
<FORM NAME="QuestionForm">
Question
<INPUT TYPE="text" NAME=txtQNumber SIZE=1>
<SCRIPT LANGUAGE=JavaScript>
document.write(getQuestion());
</SCRIPT>
<INPUT TYPE="button" VALUE="Check Question" NAME=buttonCheckQ
onclick="return buttonCheckQ_onclick()">
</FORM>
</BODY>
</HTML>
---------------------------
this is from chapter 6, if anyone has the book and wants to read about the intent of the page.
My question is: the number that appears in the text box at the top of the page is correct the first time the page is loaded, but no matter how many times I reload the number stays the same. Different questions from the question array do appear, and an alert shows that the value of questionNumber does change at random.
Q: why does the indicated number NOT change when the page is refreshed.
This happens in IE 4 and 5.5, have not tried on a Netscape machine.
Thanks, looking forward to a lot of learning from the list, hoping to contribute something useful someday
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20010709/2c784d3e/attachment.htm>
More information about the Javascript
mailing list