CSC 110 Assignment 4
Old McDonald

Due Date

Objectives

Instructions

Create a simple HTML page

Using a simple text editor (such as Notepad or Notepad++), create an HTML page. The content of this page shall be blank, initially.

Create a simple function

Update the page to include scripting. That is, put a script tag into your page.

You will now create functions to print out the children's song, "Old McDonald". First, the title.

  1. Create a function. This function will simply write out the title of the song: "Old McDonald". Name the function something descriptive. Within the function, use the document.write method to print out the name of the song as a level 1 heading.
  2. Call (use) your function to write out the title of the song.
    For example:

    songTitle();

You can test your script at this point. Congratuations! You have just created and used a function in JavaScript.

Create a function with parameters

It’s now time to create a function to write out the verses for the children's song, "Old McDonald".

  1. Create another function. This function will write out one verse of the song. Here is a sample verse:

    Old McDonald had a farm, E-I-E-I-O.
    And on that farm he had a duck, E-I-E-I-O.
    With a quack, quack here. And a quack, quack there.
    Here a quack, there a quack, everywhere a quack, quack.
    Old McDonald had a farm, E-I-E-I-O.

    where duck and quack can be replaced by other animal names and sounds. Since the type of animal and the sound it makes can be different for each verse, use two parameters (the things within the parentheses) to make this function general purpose. So, we could have created the preceding verse using the following function call.

    verse("duck", "quack");

    Note: The verses will be easier to read if you either:
    1) make the whole verse on paragraph with line breaks at the end of each line, or
    2) put in a single line break at the end of each line and two line breaks after the end of the verse.

  2. Use your functions to write out three verses of the song.
    Call your title function first; then call your verse function three times, giving the names and sounds of the animals for each verse.
    For example:

    songTitle();
    verse("duck", "quack");
    verse("cow", "moo");
    verse("chicken", "cluck");

You can test your script at this point.

Prompt the user for input

In the script, add the following:

  1. Declare two variables, one to hold the name of a kind of animal, the other to hold the sound that animal makes. Give the variables descriptive names.
  2. Ask the user for a type of animal. Store the response from the user in the appropriate variable you declared above.
  3. Ask the user what sound that animal makes. Store the response in the appropriate variable.
  4. Use the document.write method to echo these responses to the user. For example:

    You chose the animal: pig.
    The pig makes the sound: "oink".

    where pig and oink are the values supplied by the user.

  5. Use the user's input to add a fourth verse to the song.

You can test your script at this point.

Style Guidelines

For commenting:

For formatting:

Remember the style portion accounts for 20% of the base score of each assignment.

[Optional] Extra-Credit Possibilities

Here are some options for extra credit.

  1. Additional Verses [0.3 points]
    Write script to support possibly adding a fifth verse to the song:
  2. Initial Vowel [0.6 points]
    In English, the indefinite article is "a" before a consonant. It is "an" before a vowel. Update the "verse" function to create grammatically correct verses. So, it should write out:

    Old McDonald had a farm, E-I-E-I-O.
    And on that farm he had an owl, E-I-E-I-O.
    With a who, who here. And a who, who there.
    Here a who, there a who, everywhere a who, who.
    Old McDonald had a farm, E-I-E-I-O.

    instead of

    Old McDonald had a farm, E-I-E-I-O.
    And on that farm he had a owl, E-I-E-I-O.
    With a who, who here. And a who, who there.
    Here a who, there a who, everywhere a who, who.
    Old McDonald had a farm, E-I-E-I-O.

    and, similarly,

    Old McDonald had a farm, E-I-E-I-O.
    And on that farm he had a pig, E-I-E-I-O.
    With an oink, oink here. And an oink, oink there.
    Here an oink, there an oink, everywhere an oink, oink.
    Old McDonald had a farm, E-I-E-I-O.

    instead of

    Old McDonald had a farm, E-I-E-I-O.
    And on that farm he had a pig, E-I-E-I-O.
    With a oink, oink here. And a oink, oink there.
    Here a oink, there a oink, everywhere a oink, oink.
    Old McDonald had a farm, E-I-E-I-O.

    To do this, use the charAt method of String. Here is a sample of its use:

    var firstName, lastName;
    firstName = prompt("What is your first name?");
    lastName = prompt("What is your last name?");
    document.write("Hello, " + firstName + " " + lastName + ".<br>");
    document.write("Your initials are " + firstName.charAt(0)
        + lastName.charAt(0) + ".");

    Notes:

  3. Reusable Code [0.3 points]
    Within the verse function, there are several repetitive strings. For example:

    Create functions that will output these repeated strings. Use these functions to shorten the "verse" function..

Please make sure that you complete all of the main parts of the assignment before you start on either of these extra-credit options.

E-mail Assignment File

Assignments will be submitted using e-mail.

Schedule Page