Sams Teach Yourself JavaScript in 24 Hours
- Table of Contents
- Copyright
- About the Author
- Acknowledgments
- We Want to Hear from You!
- Reader Services
- Introduction
- Part I: Getting Started
- Hour 1. Understanding JavaScript
- Hour 2. Creating a Simple Script
- Hour 3. How JavaScript Programs Work
- Part II: Learning JavaScript Basics
- Hour 4. Using Functions and Variables
- Hour 5. Using Strings and Arrays
- Hour 6. Testing and Comparing Values
- Hour 7. Repeating Yourself: Using Loops
- Using for Loops
- Using while Loops
- Using do...while Loops
- Working with Loops
- Using for...in loops
- Workshop: Working with Arrays and Loops
- Summary
- Q&A
- Quiz
- Exercises
- Hour 8. Using Math and Date Functions
- Part III: The Document Object Model (DOM)
- Hour 9. Working with the Document Object Model
- Hour 10. Responding to Events
- Hour 11. Using Windows and Frames
- Hour 12. Getting Data with Forms
- Hour 13. Using Graphics and Animation
- Part IV: Moving on to Advanced JavaScript Features
- Hour 14. Creating Cross-Browser Scripts
- Hour 15. Creating Custom Objects
- Hour 16. Working with Sounds and Plug-Ins
- Hour 17. Debugging JavaScript Applications
- Part V: Working with Dynamic HTML (DHTML)
- Hour 18. Working with Style Sheets
- Hour 19. Using Dynamic HTML (DHTML)
- Hour 20. Using Advanced DOM Features
- Part VI: Putting It All Together
- Hour 21. Improving a Web Page with JavaScript
- Hour 22. Creating a JavaScript Game
- Hour 23. Creating DHTML Applications
- Hour 24. JavaScript Tips and Tricks
- Part VII: Appendices
- Appendix A. Other JavaScript Resources
- Appendix B. Tools for JavaScript Developers
- Appendix C. Glossary
- Appendix D. JavaScript Quick Reference
- Appendix E. DOM Quick Reference
Workshop: Working with Arrays and Loops
To apply your knowledge of loops, you will now create a script that deals with arrays using loops. As you progress through this script, try to imagine how difficult it would be without JavaScript's looping features.
This simple script will prompt the user for a series of names. After all of the names have been entered, it will display the list of names in a numbered list. To begin the script, initialize some variables:
names = new Array(); i = 0;
The names array will store the names the user enters. You don't know how many names will be entered, so you don't need to specify a dimension for the array. The i variable will be used as a counter in the loops.
Next, use the prompt statement to prompt the user for a series of names. Use a loop to repeat the prompt for each name. You want the user to enter at least one name, so a do loop is ideal:
do {
next = prompt("Enter the Next Name", "");
if (next > " ") names[i] = next;
i = i + 1;
}
while (next > " ");
This loop prompts for a string called next. If a name was entered and isn't blank, it's stored as the next entry in the names array. The i counter is then incremented. The loop repeats until the user doesn't enter a name or clicks Cancel in the prompt dialog.
Next, your script can display the number of names that were entered:
document.write("<h2>" + (names.length) + " names entered.</h2>");
This statement displays the length property of the names array, surrounded by level 2 header tags for emphasis.
Next, the script should display all the names in the order they were entered. Because the names are in an array, the for...in loop is a good choice:
document.write("<ol>");
for (i in names) {
document.write("<li>" + names[i] + "<br>");
}
document.write("</ol>");
Here you have a for...in loop that loops through the names array, assigning the counter i to each index in turn. The script then prints the name with a <li> tag as an item in an ordered list. Before and after the loop, the script prints beginning and ending <ol> tags.
You now have everything you need for a working script. Listing 7.2 shows a complete version of the script, including the usual HTML and <script> tags.
Example 7.2. A script to prompt for names and display them
<html>
<head>
<title>Loops Example</title>
</head>
<body>
<h1>Loop Example</h1>
<p>Enter a series of names. I will then
display them in a nifty numbered list.</p>
<script LANGUAGE="JavaScript1.2" type="text/javascript1.2">
names = new Array();
i = 0;
do {
next = window.prompt("Enter the Next Name", "");
if (next > " ") names[i] = next;
i = i + 1;
}
while (next > " ");
document.write("<h2>" + (names.length) + " names entered.</h2>");
document.write("<ol>");
for (i in names) {
document.write("<li>" + names[i] + "<br>");
}
document.write("</ol>");
</script>
</body>
</html>
When you load this document into a browser, you'll be prompted for a name. Enter several names, and then click Cancel to indicate that you're finished. Figure 7.2 shows what the final results should look like in a browser.
Figure 7.2 The output of the names example, as shown by Netscape.
Summary | Next Section

Account Sign In
View your cart