Chapter 11 Assignments


Home

Section 11.1

1. Because JavaScript uses objects to perform many tasks, JavaScript is commonly referred to as a(n) ________.
Answer: object-based programming language

2. All objects have ________ and exhibit ________.
Answer: attributes, behaviors

3. The methods of the object allow you to perform many common mathematical calculations.
Answer: Math

4. Invoking (or calling) a method of an object is referred to as ________.
Answer: sending a message to the object

5. String literals or string constants are written as a sequence of characters in ________ or ________.
Answer: double quotation marks, single quotation marks

6. Indices for the characters in a string start at _________.
Answer: 0

7. String methods ________ and ________ search for the first and last occurrences of a substring in a String, respectively.
Answer: indexOf, lastIndexOf

8. The process of breaking a string into tokens is called _________.
Answer: tokenization

9. String method _________ formats a String as a hyperlink.
Answer: link

10. Date and time processing can be performed based on the _________ or on World Time Standard’s _________.
Answer: computer’s local time zone, Coordinated Universal Time (UTC)

11. Date method _________ receives as its argument a string representing a date and time, and returns the number of milliseconds between midnight, January 1, 1970, and the specified date and time.
Answer: parse


Section 11.3 Write a javascript script for the following.

Write a script that tests as many of the Math library functions in Fig. 11.1 as you can. Exercise each of these functions by having your program display tables of return values for several argument values in an XHTML textarea.
Answer: answer to 11.03


Section 11.7

Write a script that uses random number generation to create sentences. Use four arrays of strings called article, noun, verb and preposition. Create a sentence by selecting a word at random from each array in the following order: article, noun, verb, preposition, article and noun. As each word is picked, concatenate it to the previous words in the sentence. The words should be separated by spaces. When the final sentence is output, it should start with a capital letter and end with a period. The arrays should be filled as follows: the article array should contain the articles "the", "a", "one", "some" and "any"; the noun array should contain the nouns "boy", "girl", "dog", "town" and "car"; the verb array should contain the verbs "drove", "jumped", "ran", "walked" and "skipped"; the preposition array should contain the prepositions "to", "from", "over", "under" and "on". The program should generate 20 sentences to form a short story and output the result to an XHTML textarea. The story should begin with a line reading "Once upon a time..." and end with a line reading "THE END".
Answer: answer to 11.07


Section 11.21

Write a program that reads a five-letter word from the user and produces all possible three-letter words that can be derived from the letters of the five-letter word. For example, the three-letter words produced from the word “bathe” include the commonly used words “ate,” “bat,” “bet,” “tab,” “hat,” “the” and “tea.” Output the results in an XHTML textarea.
Answer: answer to 11.21


Section 11.22

(Printing Dates in Various Formats) Dates are printed in several common formats. Write a script that reads a date from an XHTML form and creates a Date object in which to store it. Then use the various methods of the Date object that convert Dates into strings to display the date in several formats.
Answer: answer to 11.22


Section 11.29

(Project: Crossword Puzzle Generator) Most people have worked a crossword puzzle, but few have ever attempted to generate one. Generating a crossword puzzle is suggested here as a string-manipulation project requiring substantial sophistication and effort. There are many issues you must resolve to get even the simplest crossword puzzle generator program working. For example, how does one represent the grid of a crossword puzzle in the computer? Should one use a series of strings, or use double-subscripted arrays? You need a source of words (i.e., a computerized dictionary) that can be directly referenced by the program. In what form should these words be stored to facilitate the complex manipulations required by the program? The really ambitious reader will want to generate the clues portion of the puzzle, in which the brief hints for each across word and each down word are printed for the puzzle worker. Merely printing a version of the blank puzzle itself is not a simple problem.
Answer: answer to 11.29