HW 2 Ch 3 Ex Exercise for After Assignment 2 of Chapter 3

HW 2 Ch 3 Ex Exercise for After Assignment 2 of Chapter 3

HW_2_Ch_3_Ex Exercise for after assignment 2 of Chapter 3

Easy if’s and checking answers

In this program, the JS script may be in the body of the HTML page.
The program is part of a 'packing hints' site such as an airline or Expedia might put up.
Your job is to help travelers decide what size suitcase(s) they need.
Here is what the advice will be:

People going to a special event (wedding, meeting prospective in-laws etc) or taking extra athletic gear (skis, surf boards, golf clubs) are advised to take the largest suitcase they own.
Other people are advised on what size suitcase to pack depending on how long they will be away and whether they are going to a warm or cold climate. Travelers going to a warm climate for 4 days or fewer are advised to go with just a carry-on, while those going to a warm climate for more than 4 days are advised to take a medium size suit-case with no carry-on.
Travelers going to a cold climate for a short time (4 days or fewer) are advised to take a medium sized suitcase with a carryon, while those going for a longer stay are advised to take a large suitcase with a carry-on.
If your user's response is stored in a variable named answer, then answer.charAt(0) is the value of the left most (0-th) character.
You may need to test it for both lower-case and upper-case versions of a letter, or you may examine the 0-th character of answer.toUpperCase( ) or answer.toLowerCase( ), using those built in string functions.

Constructing a simple simulation

You have decided to invest some money in the stock market. Each month your return varies.
Suppose that you start with $1000 and for a period of 24 months print out the value of your investment at the end of the month.

The percent gained/lost should be returned to the body of the program by a function, and then the computation of the new balance may be done in the body for .
The rule for determining the percent gain is:

Pick a random number y between 0 and 1.

If y < 0.2 then the return that month is -.10 (10% loss)

If 0.2 <= y < 0.4 then the return that month is -.02% (2% loss)

If 0.4 <= y < 0.6 then the return that month is .01% (1% gain)

If 0.6 <= y < 0.8 then the return that month is .02% (2% gain)

If 0.8 <= y then the return that month is -.10% (10% gain)

Of course, if at the end of some month you have an investment of $d and you have a gain of .03 during the month, then at the end of the next month your invest is worth 1.03 * d.

Alphabetizing a list of words

You have an array of strings. The strings are names of cities and states and you would lilke to alphabetize them. To do this you will use the sort() method on arrays, but you will need to write your own comparison function.

Among other rules for alphabetizing, the American Library Association uses the following:

  • Upper and lower case are irrelevant for alphabetizing (e.g. “Jeans” = “jeans”)
  • A space comes alphabetically before any letter (e.g.” New York” < “Newark”)
  • Treat hyphens and apostrophes as though they were not there (e.g. “O’Reilly” =”OReilly”)

Notes:

  1. You will need to define a compare( u, v) function to decide which string comes alphabetically first using these rules.
  2. You may need to look at the strings u and v character by character until you get a decision as to which comes first or if they are equal
  3. You may NOT turn the strings into upper case, but you may choose to have a handle() function which turns a string into upper case and gets rid of the hyphens and apostrophes.
    The compare(u, v) could then work on handle(u) and handle(v).
  4. You will have to think about the condition for compare for strings of unequal length.

Here is the list of places to put in your array:

Boston, Dallas-Forth Worth, New Orleans, Cmabridge, New York City, Port Arthur,Ville Vieux, Portand, Saint Paul, Saint Louis, Saint-Paul, South York, South Beach, Southland, Ville-Marie, Villanova, Dallas