MCS-170 Project 2: Mad Libs 

Due: Wednesday, February 22

Project Task

In Project 1 we created a web page using the HTML markup language.  HTML lets a user specify the page layout of a static web document, one in which the content does not change.  A quick look at almost any commercial web page will attest to the utility of web pages that are dynamic, that change in response to user actions.  To create a dynamic web page, we will need to use a programming language to control user interaction.  In this lab we will begin to explore the JavaScript programming language and how it is used in a Web page.  We will develop an interactive Web page for generating interesting stories called Mad Libs.

In lab

  1. A good mini-reference for JavaScript is located here.  Search through this page for the answers to the following questions:

  2. Before we start typing in JavaScript code on a web page, we will discuss handling errors.  Errors can be quite hard to track down, but many Web browsers provide feedback concerning JavaScript errors, with information that helps to locate and identify these errors.  (!! Before continuing, make sure your browser has JavaScript enabled. To check this in the Firefox browser, go to the "Preferencs..." submenu under the "Edit" menu. A dialog box will pop up. On the left side of this box, click on the "Web Features" icon.  In the panel on the right, the checkbox labeled "Enable Javascript" should be checked.  If it isn't, just click on it.)

    For example, the web page ErrorDemoPage.html contains the following mistyped JavaScript statement:

    document.write("Hello world);

    Loading the ErrorDemoPage.html page in a browser will cause a JavaScript error (Try this). Depending on which browser you are using, and how it is configured, an error message may appear in a dialog box, or you may simply see a blank browser window.

    On Firefox, when you load the ErrorDemoPage, you will see a blank browser window. When this happens you can go to the Tools menu and select JavaScript Console. When you do this a window will be displayed.  Scroll down to the last error message:

    Error: unterminated string literal
    Source File http://www.gac.edu/~hvidsten/courses/MC170/ErrorDemoPage.html
    document.write("Hello world);
    ---------------'

    As you can see, the error message not only indicates the type of error that occurred (e.g. "unterminated string literal") it also indicates where the error occured. If you click on the link beside "Source File", a window will open containing the source for the page and the error will be highlighted. Note that you will not be able to edit the source in this window. Instead, to correct the error you will need to edit your original HTML document (using KWrite), save it and reload it in the browser.



Mad Libs (from Dave Reed's course at Dickinson College - 2004)

For the last part of this lab, you are to write a JavaScript Mad Lib. If you have never seen a Mad Lib before, the concept is pretty straightforward. A potentially humorous story is written down, with blanks in the place of some of the key words. Before reading the story, the storyteller asks others present to fill in those blanks. Those selecting the words are only told the type of word required, and have no other knowledge of the story. This lack of context in selecting words can result in an entertaining story when the words are plugged in to the appropriate places.

Consider a simple example:

It was a  adjective  kind of day when  person's name  walked out into the street. The sky was a deep  color , and  same name  was walking his new pet  animal  ...

Making the following substitutions:

adjective = smarmy
person's name = Chris
color = mauve
animal = gnu

the story would read:

It was a smarmy kind of day when Chris walked out into the street. The sky was a deep mauve, and Chris was walking his new pet gnu ...

Now that you know how to create interactive Web pages using HTML and JavaScript, you can write a simple page that generates Mad Libs.  To do this, you are to create a Web page 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. Your code should then display the story in the Web page, using the values of the variables where appropriate.  Refer to Chapter 4 for the basics of prompting the user for text and writing text to a web document.

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

Include the complete HTML source for your MadLib page as your answer for this exercise.

Project Report

The project report should summarize the work done for this lab.  We have included a sample project report here for you to use as a guide in preparing your report.  To get some sense of how this project report will be graded, look at the grading sheet that we use. Your report should have all HTML and JavaScript code for your MadLibs page attached as an appendix. Make sure that you consider the College's Honor Policy and write the following in full and sign it on your report:

On my honor, I pledge that I have not given, received, nor tolerated others' use of unauthorized aid in completing this work.


Back to MCS 170 home page