### CS 10 Homework 8

Due by the beginning of class Thursday, April 3

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

1. Create a Web page called Swapper.html that looks like the page shown below. After the user enters values in Field A and Field B, clicking the Swap Values button should cause the values shown in the fields to be swapped.

2. Case Problem 1 (Kelsey's Diner) on page 8.44 of your HTML book. The relevant files are in the Kelsey subfolder. This problem asks you to first create two JavaScript functions, DishName and DishDesc, which will use arrays to store information about menu items. DishName will take a number from 0 to 6 (representing a weekday), and return the name of that day's special. For example, calling DishName(0) would return the string "Chicken Burrito Amigo". DishDesc will take a weekday number and return the full description of the dish. For example, DishDesc(0) would return "Chicken with mushrooms, onions, and Monterey Jack cheese wrapped in a flour tortilla. 9.95".

You can then use these two functions in your main program to generate the HTML code for the daily special, based on the current weekday. To find out the weekday, just use a Date object. Your main program code should go between the <script> tags appearing after the <h4>Today's Special</h4> heading. Don't forget to include <dl>, <dt>, and <dd> tags to format the daily special appropriately. To reduce the amount of typing, you can cut-and-paste the names and descriptions of the daily specials from the file menutext.txt.

For testing purposes, you may want to create Date objects for particular fixed dates (as opposed to the actual current date), so that you can be sure your program works for each day of the week.

3. Case Problem 2 (Madison State College) on page 8.46. The relevant files are in the Twain subfolder. This problem is similar to Kelsey's Diner. You will need to create a function called MQuote that selects a quote from a list of available quotes stored in an array. Another function, called RandInt, will return a random whole number from 0 to some specified maximum value. You can then use RandInt in conjunction with MQuote to select a quote to display on the page.

4. Add the following buttons to Calculator.html:

• F->C for converting Farenheit to Celsius. For example, entering "212" into the calculator and pressing F->C should cause 100 to be displayed. To do this, define a function called FtoC() that gets called when the F->C button is clicked. This function does not need to take any input parameters, because it will get its input directly from the calculator display. The function should retrieve the current value from the display, convert it to Celsius, and then update the display.

• C->F for converting Celsius to Farenheit.

• n! for computing the factorial of a number. The factorial of a number n is just the product of 1 × 2 × 3 × ... × n. As a special case, the factorial of 0 is defined to be 1. Your function should use a loop to calculate the factorial value (either a while-loop or a for-loop will work). Make sure that your n! button works correctly for 0.

5. The file Mystery.html contains three 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.

• mystery1(3)
• mystery1(5)
• mystery2(3, 5)
• mystery2(4, 0)
• mystery3(9, 4)
• mystery3(3, 3)

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

6. EXTRA CREDIT: Unfortunately, the calculator still has problems with the decimal point. It is possible to enter more than one decimal point for a single number. For example, you can enter "123.45.6" (try it). Modify the calculator code to correct this problem.

One way to approach this is to introduce an extra global variable that stores a true or false value, indicating whether or not the decimal key has been pressed. This variable is initially false, but can be set to true the first time the decimal key is clicked. Then, if the key is clicked again, pressed will know that the decimal point has already been entered, so it can just ignore the key click. Remember, however, to reset the variable back to false at the appropriate time, so that the decimal point can be used again for the next number.