CSC 110 Assignment 4
Old McDonald |
Due Date
- Friday, July 24, 2009, 9:00 pm.
Objectives
- Create a simple JavaScript function.
- Use parameters to customize a function.
- Call a function with parameters.
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.
- 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.
- 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
Its now time to create a function to write out
the verses for the children's song, "Old McDonald".
- 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.
- 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:
- 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.
- Ask the user for a type of animal. Store the response from the user in the appropriate
variable you declared above.
- Ask the user what sound that animal makes. Store the response in the appropriate
variable.
- 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.
- 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:
- A comment shall appear in the head of the html. This
comment shall minimally contain your name and this assignment name and number.
- A comment shall precede each of the sections of this
assignment. If you complete any of the extra credit options, an HTML
comment shall precede each of these, as well. These comments shall
minimally contain the name of the section.
For formatting:
- The HTML code for each of the sections of the assignment
shall be separated from the other sections by at least two blank lines.
- Please minimize the use of double blank lines within the
sections.
- There shall be no more than two adjacent blank lines,
except at the end of the file. Multiple blank lines following the
closing HTML tag are acceptable.
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.
- Additional Verses [0.3 points]
Write script to support possibly adding a fifth verse to the song:
- Write a JavaScript function that will ask the user for the name of an animal and the
sound that it makes.
- After collecting this information, call the "verse" function for the song,
using the values supplied by the user.
Notice this function does not need any parameters. You can copy and paste a large part of
this function from the work you have already completed.
- Use the window.confirm method to ask the user if she/he wants to add another verse to
the song.
- If the user clicked OK, call the function you just wrote to query the user for input and
to output another verse of the song.
- 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:
- The charAt method counts characters starting at zero (0).
- The vowels of English are "a", "e", "i", "o",
"u", "A", "E", "I", "O", and
"U". The upper-case and lower-case letters are different.
- Remember to check for equality, use double equal signs (==), not a single equal sign
(=).
- To combine several comparisons, you must use do it the "long way":
if(favColor == "red" || favColor == "blue" ||
favColor == "green")
{
- Reusable Code [0.3 points]
Within the verse function, there are several repetitive strings. For example:
- Old McDonald had a farm
- E-I-E-I-O
- an oink
- an oink, oink
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.
- Subject line: CSC 110 Assignment
- Body, required contents:
- Your full name
- This assignment name and number:
Old McDonald, four (4)
- List any extra-credit options you want graded
- Attachment: The completed HTML file.
- Email to:
djinguji@sccd.ctc.edu
Schedule Page