CS 10 Homework 9

Due by the beginning of class Tuesday, April 8

The relevant files for this homework assignment are available in the CS 10 class folder under Homework/HW9, or as a downloadable .zip file here.

1. Work through Tutorial 9 in your HTML book. The relevant files are in the GPSware subfolder.

2. Case Problem 1 (Monroe Public Library) on pages 9.50-51 of your HTML book. The relevant files are in the Monroe subfolder.

3. Case Problem 2 (Games Etc.) on pages 9.52-53. The relevant files are in the Games subfolder.

4. Suppose we define two JavaScript arrays as follows:

```var smallNames = new Array("zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine", "ten", "eleven",
"twelve", "thirteen", "fourteen", "fifteen", "sixteen",
"seventeen", "eighteen", "nineteen");

var bigNames = new Array("twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety");
```
Using these arrays, write a JavaScript function called NumberName that takes a single number from 0 to 99 as input and returns the full English name of the number as a string. Some examples are shown below:

• NumberName(3) should return the string "three"
• NumberName(19) should return the string "nineteen"
• NumberName(21) should return the string "twenty-one"
• NumberName(99) should return the string "ninety-nine"
• NumberName(50) should return the string "fifty"

HINT: If the function's input value (let's call it n) is less than 20, just retrieve the appropriate name directly from the smallNames array, using n as the array index. For bigger values, first break n into its "ones" and "tens" components, and then assemble the final string by using these values to select the appropriate words from the arrays. The "ones" component of n is just the remainder when n is divided by 10. To compute the "tens" component, divide by 10 and round down the result to the next smallest whole number. You can use JavaScript's built-in Math.floor(x) function to round down a number. For example, if n is 48, n % 10 will produce 8 (the "ones" digit), and Math.floor(n / 10) will produce 4 (the "tens" digit).

Use the file Numbers.html as your starting point for this problem. Just fill in the code for the NumberName function in the file.

5. The file Mystery2.html contains five more JavaScript mystery functions that involve while-loops or for-loops. Print out a copy of this file and work through the execution of the six function calls listed below by hand in order to determine the final value of each call. For each one, draw a table on a piece of paper showing how the values of the relevant variables change at each step during the process. Feel free to test out your answers on the computer if you like, but only after having worked through each call on paper.

• mystery4(5)
• mystery5(10)
• mystery6(7, 2)
• mystery7(5)
• mystery8(6, 1)
• mystery8(3, 3)

For each mystery function, choose a better name for it that describes what the function computes.