New Perspectives on HTML, CSS, and Dynamic HTML 5th edition Tutorial 11 Case 3 Kiddergarden
Kiddergarden Pete Burnham manages the Web site kiddergarden, a family-friendly site containing games, puzzles, stories, and other activities. Pete has asked for your help in developing a Web page containing a number guessing game.
Pete needs you to create a script that generates a random integer between 1 and 500. A user has up to 10 guesses to guess the number. After each guess, the page will tell the user whether his or her guess is too high, too low, or exactly correct. A preview of the page is shown in the Figure 11-40.
You will be provided with a function named checkEndGame() that displays an alert box telling the user whether he or she has won the game by guessing the number within the allotted number of guesses, or has a lost by failing to guess the correct number. The rest of the code is left for you to complete.
Complete the following:
1. Use your text editor to open the guesstxt.htm file from the tutorial.11\case3 folder included with your Data Files. Enter your name and the date in the comment section of the file and save it as guess.htm. Take some time to study the content and structure of the document, including the elements within the guessform web form.
2. Within the script element, declare the following four global variables: lowest, size, randNum, and guess. The lowest variable will be used to store the lowest possible random integer in the guessing game. The size variable will store the number of random integers in the game. The randNum variable will store the random number generated by the Web page. The guess variable will store the users current guess. Set the initial values of the lowest and size variable to 1 and 500, respectively. Do not set the initial values for the randNum and guess variables.
3. Below the four variable declarations you just created, insert the generateRandom() function. The function, will be used to generate a random number, has no parameters. It should contain a single statement to set the value of randNum variable to a random integer between the values of the lowest and size variables. (Hint: Use the code describing how to generate random integers presented in the tutorial to aid you in writing this function.)
4. Insert a function named reportResult() that display the result of each guess. The function has no parameters and should include the following commands:
a. Set the value of the guess variable to the value of the guess field in the guessform Web form. Use the eval() method to convert the text in the field to a numeric field.
b. Declare a variable named result that will contain the result of the users guess. Use nested conditional operator to set the value of result to the text right! if guess equals randNum; if guess is greater than randNum, set the value of result to the text string too high; otherwise, set the value result to the text string too low.
c. Reduce the value of the guesses field in the guessform Web form by 1 to indicate that the user has one fewer guess left.
d. Set the value of result field in the guessform Web form to the value of the result variable.
e. Call the checkEndGame()function to test whether the game has been concluded by the user.
5. Create a function named resetGame(). The purpose of this function is to reset the game with a new random number and a new set of guesses. The function has no parameters and should include the following commands:
a. Set the value of the guesses field in the guessform Web form to 10.
b. Set the value of the guess field in the guessform Web form to an empty text string (“”).
c. Set the value of the result field to an empty text string (“”).
d. Call the generateRandom() function.
6. Add an onload event handler to thetag to run the generateRandom() function when the browser initially loads the page.
7. Add an onclick event handler to the input element for the guess button to run the reportResult() function when the button is clicked.
8. Add an onclick event handler to the Play Again button to run the resetGame() function when the button is clicked.
9. Save your changes to the file, and then load the guess.htm in your Web browser. Verify that the page generates a random number, and that it reports whether your guess is too high, too low, or correct when you enter a number in a guess field and click the Guess button. Further verify that the number of guesses remaining counts down by 1 starting from 10 and going to 0.
10. Submit your completed project to your instructor, in either printed or electronic form, as requested.