Wrox Programmer Forums
|
BOOK: Beginning JavaScript
This is the forum to discuss the Wrox book Beginning JavaScript by Paul Wilton; ISBN: 9780764544057
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning JavaScript section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 28th, 2003, 03:12 PM
Registered User
 
Join Date: Jul 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default TRIVIA QUIZ

:)
Hi,
Sorry if my English is not good, I'm from Brazil and my English is so so.

I bought the book "Beginning JavaScript" in my country (Brazil) and now I'm with a trouble.
My question is about Trivia Quiz; I'm having difficult in do the program run in my computer. I have worked with Front Page and when I put the strings in the html space and I have seeing the result, which appear a message that is written: "script error". I don't know why appear it. Because I put the same string of the book.

<HTML>
<HEAD>
<TITLE>Wrox Online Trivia Quiz</TITLE>

<SCRIPT LANGUAGE=JavaScript>

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

</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>

</BODY>
</HTML>

The message error says that I'm missing to put ";" in line 12 character 9.
Why appear it, if is the same string of the book and what I may do in this case?
Thanks a lot!

[u]Rodrigo</u>

 
Old July 30th, 2003, 07:44 PM
Registered User
 
Join Date: Jul 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Although I'm just a beginner myself, I was surprised to see the conditional "IF" statement not followed by curly brackets. What makes sense to me is to see it as such:

function answerCorrect(questionNumber, answer)
{
   var correct = false;

   if (answer == answers[questionNumber])
   {
   correct = true;
   }

   return correct;
}

Maybe someone can explain why this conditional "if" doesn't require curly brackets. I think the reason you were getting the error message was due to the missing curly brace - hence, the browser thought it was a statement requiring an end ; - not a conditional.










Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.