JavaScript Lab
You are to write 2 programs in the lab.
Anything not finished in lab is to be finished for HW
First Program
In this program, the JS script is to be in the body of the XHTML 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.
Second Program
In this program the JS is to be in one or two external files.
This program has a function which rounds to a number of the user's choice to a
desired number of places. The user should be asked for two pairs of
number-to-round and how many places s/he wishes to round to.
Math.round(num)
will round the number num to the nearest whole number.
(Note that Math starts with a capital letter - you are using the built-in Math
object).
Math.pow(x,y) returns x
raised to the y. (e.g. Math(10,3) returns 1000).
To round num to 3 decimal places, you round 1000*num to the nearest integer and
then divide the result by 1000. If you have never seen this before, you
should convince yourself that it works.
Be sure to test your code on both positive and negative numbers and on a
variety of powers. For example, -3.5 rounded to 0 places should give
-3 and +3.5 rounded to 0 places should, of course, give 4.
Math.pi is the obvious constant - should you wish to
use it for rounding.