In a moment, I’ll show you how to do something really fun with a for loop— namely, reverse the text entered by the user. But before we get there, I need to show you a preliminary example.
In Chapter 3, “ Using Conditional Statements,” I showed you how to display an alert box, which provides a message to the user.
A prompt box provides a similar sort of facility except that it also lets the user enter input.
By the way, you should expect the appearance and user prompt to vary in different browsers. I show Internet Explorer in these figures, but things won’t quite look the same in Mozilla or Netscape.
ReturnValue = prompt (message, default text);
For example, this assignment:
retVal = prompt("Enter your text, please!", "How doth the little crocodile?");
produces the prompt box shown in Figure 4-1.
Figure 4-1: The prompt function displays a box that accepts user input.
Now that you know how prompt boxes work, let’s go ahead and put them to use in conjunction with a loop.
A prompt box is modal, meaning that no further program statements will be executed until the user chooses OK or Cancel. Another way of putting this is that all other code statements in the program are blocked until the user takes action.