|
Book Section


Teach Yourself JavaScript 1.5

General Section

Other Sites

|
| Answers to Exercises - Teach Yourself JavaScript 1.5 (Second Edition) |
The exercises at the end of each hour of Teach Yourself JavaScript 1.5 (Second Edition)
are listed below, with answers where appropriate. Answers appear in
green bold text below each exercise, and often link to example pages - view the source
of these pages for the complete answer.
NOTE, 1/29/2002: The answers for Parts I and II are now online.
[Skip to Hour
24]
Part I: Getting Started
Hour 1: Understanding JavaScript
- Exercise 1: Visit this book's Web site...
- Answer: (self-explanatory)
- Exercise 2: Visit Netscape's Developer Web site...
- Answer: (self-explanatory)
Hour 2: Creating a Simple Script
- Exercise 1: Add HTML Comments to the Date and Time script, as described in the
Workshop section.
- Exercise 2: Add a millisecond field to the large clock.
- Exercise 3: Change the large clock to display UTC.
Hour 3: Exploring JavaScript's Capabilities
- Exercise 1: Browse the Web and see if you can find examples of each of the uses of JavaScript mentioned during this hour.
- Answer: (self-explanatory)
- Exercise 2: If you use Netscape, try typing about:plugins in the Location field.
- Answer: (self-explanatory)
Hour 4: How JavaScript Programs Work
- Exercise 1: Examine the Date and Time script you created in Hour 2 and find any examples of functions and objects being used.
- Answer: In the Date and Time script, the now variable is an object
that stores a date. Several functions are used in this script: Date(), to create the Date object, now.toString() and now.toGMTString(),
methods of the Date object, and document.write().
- Exercise 2: Add comments to the Date and Time script to make it more clear what each line does.
Part II: Learning JavaScript Basics
Hour 5: Using Variables and Functions
- Exercise 1: Modify the Greet function to accept two parameters, who1
and who2, and to include both names in a single greeting dialog. Modify Listing 5.4 to use a single
function call to the new function.
- Exercise 2: Add the Average function to the header of a simple HTML document. Add a
function call that uses the alert statement to display the average and verify that the result is correct.
- Exercise 3: Modify Listing 5.6 by removing the var keyword before name2
in the Greet function. Are the greetings different from the original version?
- Answer: Here is the Modified Listing 5.6.
Because the var keyword isn't used, the statement name2="Barney" in the function changes the
global name2 variable
. Thus, when the function is called, the names "Fred" and "Barney" are displayed.
In the original Listing 5.6, since var is used,
the name2 assignment within the function creates a separate local variable, and doesn't affect
the greetings displayed in the alert boxes.
- Exercise 4: Add some additional customizable features to Listing 5.7.
For example, you could prompt the user for a URL and include a link on the page.
- Answer: You could modify this example in many ways, but here's
my version.
Hour 6: Using Strings and Arrays
- Exercise 1: Use Netscape's JavaScript console to create several strings and experiment with them.
- Answer: (self-explanatory)
- Exercise 2: Modify the scrolling message example
to scroll in the opposite direction.
- Answer: This is easier than it sounds. All you need to do is start the pos
variable at msg.length instead of zero, decrement (pos--) instead of increment, and modify the if
statement to return pos to msg.length when it reaches zero. Here's the complete
reverse scrolling message example.
Hour 7: Testing and Comparing Values
- Exercise 1: Modify Listing 7.5 to be case-insensitive.
- Exercise 2: Modify the script in Listing 7.5 so that
if a keyword does not match one of the defined ones, the text the user entered will be used as a URL.
Hour 8: Repeating Yourself: Using Loops
- Exercise 1: Modify Listing 8.7
to sort the names in alphabetical order before displaying them.
- Exercise 2: Modify Listing 8.7
to prompt for exactly ten names. What happens if you click the Cancel button instead of entering
a name?
- Answer: Here is the 10 name example
using a for loop instead of the original do...while loop. If you click Cancel, it counts as one of the ten
names in the loop, but doesn't add a name to the list, because the if statement within the loop
checks for valid names.
Part III: Moving on to Advanced JavaScript Features
Hour 9: Using Built-in Objects
- Exercise 1: Modify the random number script in Listing 9.3
to prompt the user for the number of random numbers to generate.
- Exercise 2: Modify the random number script to run three times, calculating a total of 15,000 random numbers,
and display separate totals for each set of 5,000.
Hour 10: Working with the Document Object Model
- Exercise 1: Modify the Back and Forward example in
Listing 10.2 to include a Reload
button along with the Back and Forward buttons.
- Exercise 2: Modify the Back and Forward example to display the current number of history entries.
Hour 11: Creating Custom Objects
- Exercise 1: Modify the definition of the Card object to include a property called email
for the person's email address. Modify the PrintCard function to include this property.
- Exercise 2: Make a script that adds a first5 method, which returns the first
five characters, to the String object.
Hour 12: Responding to Events
- Exercise 1: Add several additional links to the document in Listing 12.3.
Include event handlers that display a unique description for each link.
- Exercise 2: Modify Listing 12.3
to display a default welcome message in the status line whenever a description isn't being displayed.
Part IV: Working with Web Pages
Hour 13: Using Windows and Frames
Hour 14: Getting Data with Forms
Hour 15: Using Graphics and Animation
Hour 16: Creating Cross-Browser Scripts
Part V: Scripting Advanced Web Features
Hour 17: Working with Style Sheets
Hour 18: Creating Dynamic Pages with the DOM
Hour 19: Using Advanced DOM Features
Hour 20: Working with Multimedia and Plug-ins
Part VI: Putting it All Together
Hour 21: Finding and Handling Errors
Hour 22: Improving a Web Page with JavaScript
Hour 23: Creating a JavaScript Game
Hour 24: Where Do We Go From Here?
|
(c) 1997-2002 Starling Technologies and
Michael Moncur. Portions (c) Sams Publishing.
|
|
|