A Balanced Introduction to Computer Science and Programming

David Reed
Creighton University

Copyright © 2004 by Prentice Hall



Chapter 4: Dynamic Web Pages via JavaScript
Solutions to odd numbered exercises



EXERCISE 4.1:   Cut-and-paste the greet.html text into a new Web page and verify that it behaves as described. In particular, what is displayed if the user clicks OK without entering a value in the input area?

The page behaves as described. If the user does not enter a name, the default value (the empty string) will be used, resulting in the message "Hello , welcome to my Web page."


EXERCISE 4.3:   Each of the following is invalid as a JavaScript variable name. Explain why.

2hotforU salary$ two words "sum_to_date" name

2hotforU    does not start with a letter
salary$    contains an illegal character, '$'
two words    contains an illegal character, a space
"sum_to_date"    has quotes around it, making it a string
name    is a reserved word


EXERCISE 4.5:   Cut-and-paste the food.html text into a new Web page and verify that it behaves as described. In particular, what is displayed if the user clicks OK without entering values at the prompts?

The page behaves as described. If the user does not enter a food at the prompts, the default value "chocolate" will be displayed as their favorite food, while the default value "brussels sprouts" will be displayed as their least favorite food.


EXERCISE 4.7:   Cut-and-paste the oldmac.html text into a new Web page and verify that it behaves as described.

The page behaves as described.


EXERCISE 4.9:   Modify your oldMac.html page so that it stores the refrain in a variable and then uses that variable in the write statements. That is, add the assignment refrain = "E-I-E-I-O"; at the very top of the JavaScript code (just after the opening SCRIPT tag) and modify the write statements as in the following: document.write("<p>Old MacDonald had a farm, " + refrain + ".<br />"); Since the spelling assigned to the refrain variable is the same as before, the behavior of the page should not change.


<html> <!-- oldmac.html Dave Reed --> <!-- --> <!-- Web page that displays a verses of old MacDonald --> <!------------------------------------------------------> <head> <title> Old MacDonald </title> </head> <body> <center><h1> Old MacDonald had a Farm </h1></center> <hr /> <script type="text/javascript"> refrain = "E-I-E-I-O"; animal = prompt("Enter an animal name", "cow"); sound = prompt("What sound does it make?", "moo"); document.write("<p>Old MacDonald had a farm, " + refrain + ".<br />"); document.write("And on that farm he had a " + animal + ", " + refrain + ".<br />"); document.write("With a " + sound + "-" + sound + " here, and a " + sound + "-" + sound + " there,<br />"); document.write("&nbsp;&nbsp; here a " + sound + ", there a " + sound + ", everywhere a " + sound + "-" + sound + ".<br />"); document.write("Old MacDonald had a farm, " + refrain + ".</p>"); animal = prompt("Enter an animal name", "horse"); sound = prompt("What sound does it make?", "neigh"); document.write("<p>Old MacDonald had a farm, " + refrain + ".<br />"); document.write("And on that farm he had a " + animal + ", " + refrain + ".<br />"); document.write("With a " + sound + "-" + sound + " here, and a " + sound + "-" + sound + " there,<br />"); document.write("&nbsp;&nbsp; here a " + sound + ", there a " + sound + ", everywhere a " + sound + "-" + sound + ".<br />"); document.write("Old MacDonald had a farm, " + refrain + ".</p>"); animal = prompt("Enter an animal name", "duck"); sound = prompt("What sound does it make?", "quack"); document.write("<p>Old MacDonald had a farm, " + refrain + ".<br />"); document.write("And on that farm he had a " + animal + ", " + refrain + ".<br />"); document.write("With a " + sound + "-" + sound + " here, and a " + sound + "-" + sound + " there,<br />"); document.write("&nbsp;&nbsp; here a " + sound + ", there a " + sound + ", everywhere a " + sound + "-" + sound + ".<br />"); document.write("Old MacDonald had a farm, " + refrain + ".</p>"); </script> </body> </html>


EXERCISE 4.11:    Create a Web page named madlib.html that serves as an interactive Mad Lib program. Your page should contain JavaScript code that prompts the user for words to fill in the blanks in a story, and then stores those words in variables. After having read in all of the words, your code should then display the story in the Web page, using the values of the variables where appropriate.

The content of the story can be anything that you like -- be creative! Your story must meet the following conditions, however.

<html> <!-- madlib.html Dave Reed --> <!-- --> <!-- Web page that displays a Mad Lib story. --> <!-----------------------------------------------------> <head> <title> Dave's Mad Lib </title> </head> <body> <script type="text/javascript"> adjective = prompt("Enter an adjective", "smarmy"); boy = prompt("Enter a boy's name", "Chris"); color = prompt("Enter a color", "mauve"); animal = prompt("Enter a type of animal", "gnu"); swear = prompt("Enter a swear word or exclamation", "Dagnabit"); building = prompt("Enter a type of building", "teepee"); document.write("<center><h1>A Typical Day</h1>" + "<h3>by Dave Reed</h3></center>" + "<hr />"); document.write("<p>It was a " + adjective + " kind of day when " + boy + " walked out into the street. The sky was a deep " + color + " and " + boy + " was walking his pet " + animal + ".</p>"); document.write("<p>'" + swear +"' exclaimed " + boy + " as he stepped off" + " the curb and into a large pile of " + animal + " excrement." + " 'I think I'll just go find a nice " + building + ", crawl in," + " and go back to sleep.'</p>"); </script> </body> </html>