<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 5.50.4522.1800" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV><FONT face=Arial size=2>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)</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>the code, from their site, is</FONT></DIV>
<DIV><FONT face=Arial
size=2>-------------------------------------------------</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2><HTML><BR><HEAD><BR><TITLE>Wrox
Online Trivia Quiz</TITLE></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2><SCRIPT LANGUAGE=JavaScript></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>var questionNumber;</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>function answerCorrect(questionNumber,
answer)<BR>{<BR> // declare a variable to hold return
value<BR> var correct = false;<BR> <BR> //
if answer provided is same as correct answer then correct variable is
true<BR> if (answer ==
answers[questionNumber])<BR> correct =
true;<BR> <BR> // return whether the answer was correct
(true or false)<BR> return correct;<BR>}<BR>function
getQuestion()<BR>{<BR> questionNumber = Math.floor(Math.random() *
(questions.length));<BR> var questionHTML = "<P>" +
questions[questionNumber][0] + "</P>";<BR> var questionLength
= questions[questionNumber].length;<BR> var
questionChoice;</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> for (questionChoice = 1;questionChoice
< questionLength;questionChoice++)<BR>
{<BR> questionHTML = questionHTML + "<INPUT
TYPE=radio NAME=radQuestionChoice"<BR> if
(questionChoice == 1)<BR>
{<BR> questionHTML =
questionHTML + " CHECKED";<BR>
}<BR> questionHTML = questionHTML +
">";<BR> questionHTML = questionHTML +
questions[questionNumber][questionChoice];<BR>
questionHTML = questionHTML + "<BR>";<BR> }</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> document.QuestionForm.txtQNumber.value
= questionNumber + 1;<BR> return questionHTML;<BR>}</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>function buttonCheckQ_onclick()
<BR>{<BR> var answer = 0;</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> while
(document.QuestionForm.radQuestionChoice[answer].checked != true)
<BR> {<BR> answer++;<BR>
}</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> answer = String.fromCharCode(65 +
answer);</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2> if
(answerCorrect(questionNumber,answer) == true)<BR>
{<BR> alert("You got it right");<BR>
}<BR> else<BR> {<BR>
alert("You got it wrong");<BR> }</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>
window.location.reload();<BR>}</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2></SCRIPT><BR></HEAD></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2><BODY><BR><SCRIPT
LANGUAGE=JavaScript></FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>// Questions variable will holds questions<BR>var
questions = new Array();<BR>var answers = new Array();</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>// define question 1 <BR>questions[0] = new
Array();</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>// the question<BR>questions[0][0] = "The Beatles
were";</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>// first choice<BR>questions[0][1] = "A sixties
rock group from Liverpool";</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>// second choice<BR>questions[0][2] = "Four
musically gifted insects";</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>// third choice<BR>questions[0][3] = "I don't know
- can I have the questions on Baseball please";</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>// assign answer for question 1 <BR>answers[0] =
"A";</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>// define question 2<BR>questions[1] = new
Array();<BR>questions[1][0] = "Homer Simpson's favorite food
is";<BR>questions[1][1] = "Fresh salad";<BR>questions[1][2] =
"Doughnuts";<BR>questions[1][3] = "Bread and water";<BR>questions[1][4] =
"Apples";</FONT></DIV>
<DIV><FONT face=Arial size=2></FONT> </DIV>
<DIV><FONT face=Arial size=2>// assign answer for question 2<BR>answers[1] =
"B";</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>// define question 3<BR>questions[2] = new
Array();<BR>questions[2][0] = "Lisa Simpson plays which musical
instrument";<BR>questions[2][1] = "Clarinet";<BR>questions[2][2] =
"Oboe";<BR>questions[2][3] = "Saxophone";<BR>questions[2][4] = "Tubular Bells";
</FONT></DIV>
<DIV> </DIV>
<DIV><FONT face=Arial size=2>// assign answer for question 3 <BR>answers[2] =
"C";</FONT></DIV>
<DIV> </DIV><FONT face=Arial size=2>
<DIV><BR></SCRIPT></DIV>
<DIV> </DIV>
<DIV><FORM NAME="QuestionForm"></DIV>
<DIV> </DIV>
<DIV>Question<BR><INPUT TYPE="text" NAME=txtQNumber SIZE=1></DIV>
<DIV> </DIV>
<DIV><SCRIPT LANGUAGE=JavaScript><BR>
document.write(getQuestion());<BR></SCRIPT></DIV>
<DIV> </DIV>
<DIV><INPUT TYPE="button" VALUE="Check Question" NAME=buttonCheckQ
<BR> onclick="return
buttonCheckQ_onclick()"><BR></FORM></DIV>
<DIV> </DIV>
<DIV><BR></BODY><BR></HTML><BR>---------------------------</DIV>
<DIV>this is from chapter 6, if anyone has the book and wants to read about the
intent of the page.</DIV>
<DIV> </DIV>
<DIV>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. </DIV>
<DIV>Q: why does the indicated number NOT change when the page is
refreshed.</DIV>
<DIV> </DIV>
<DIV>This happens in IE 4 and 5.5, have not tried on a Netscape machine.</DIV>
<DIV> </DIV>
<DIV>Thanks, looking forward to a lot of learning from the list, hoping to
contribute something useful someday</FONT></DIV></BODY></HTML>