IT350 - Web and Internet Programming
Fall 2005

# Lab 06 - The Smart Money

## Introduction

You’ve been offered a “starter loan” of \$20,000. This is a fantastic opportunity, but what are you going to do with that money? This week's lab will help you explore three options: investing, casino games, and the lottery. We will, of course, be doing this all with Javascript!

## Requirements

You must create a folder on your web drive called "Lab06" (without the quotes) and store your work in that directory. This lab does NOT draw on the previous labs, so there is no need to copy your old web pages into this directory.

This week you will make a single new web page, money.html, such that:

1. Your page has a prelude declaring it as an XHTML document, and it roughly follows XHTML rules.  Validation is encouraged but not required.
2. Your page is reasonably appealing to look at (e.g. using styles, colors, etc). Your page should be tasteful and easy to read.
3. When finished, you will have a page that looks something like this when it is first loaded:

You do not have to make it look exactly like this (colors etc.) but the general layout should be similar.
4. The page will have a single button that, when clicked, performs all of the calculations described below. It is strongly recommended that you first make the HTML for the left column only, and get the JavaScript working for just this column (“investing”), then later handles the “craps” and “lottery” columns.  When complete, here is one sample output after clicking the “Calculate Results” button:
5. The leftmost column shows what would happen if you invest your money. The user will provide the annual interest rate earned on the money (initially \$20,000) in an input field. You will have four additional input fields below the interest rate which, when the calculate button is hit, will be populated with the value of your investment after 10, 20, 30, and 40 years, respectively. The formula for computing the value of your investment over time is:

Value = Principal * e(rate * time)

(Hint: There are some handy functions in the Math object that will help you – see textbook section 12.3)

Your page should generate an error if the user specifies an interest rate less than or equal to 0 or greater than 0.2 (20%). Additionally, your page should not perform any calculations (e.g., do nothing for the investing, craps, or lottery columns) until the user enters a legal value.

1. The middle column represents what would happen if you played craps with your money. The user will provide the amount bet on each hand in an input field. You will have four additional input fields below the bet amount which, when the calculate button is hit, will be populated with the amount of money you have after 10, 100, 500, and 1,000 games. Labels these fields appropriately! If at any time the amount of money you have becomes zero (or less!), it should remain zero for the rest of the games (i.e. it's impossible to go into debt).

Do not simulate the whole craps game here (but see the extra credit). The odds of winning each individual craps game is 49.3%. If the player wins, their cash increases by the bet amount. If they lose, the bet amount is subtracted from their cash.

Your page should generate an error if the user specifies a bet less than or equal to \$0 or greater than \$20,000. Additionally, your page should not perform any calculations until the user enters a legal bet value.
2. You will have a section representing what would happen if you played the lottery with your money. There is no user input for this section. You will have four input fields which, when the calculate button is hit, will be populated with the amount of money you have after purchasing 500 tickets, 1,000 tickets, 10,000 tickets, and 20,000 tickets respectively. Labels these fields appropriately! Each ticket cost \$1. To win the lottery, you must choose six numbers out of 54 correctly, thus making the odds of winning: 1 / (54 * 53 * 52 * 51 * 50 * 49), or 1/18,595,558,800. The lottery prize is \$35 million dollars.
3. You should have separate functions for the lottery, craps, and investment computations. Writing other functions may save you some work too!

HINTS:

1.      You do NOT want to create all of the HTML for the table, input boxes etc. using JavaScript.  Instead almost all of this will be done with HTML in the <body> section.  Then you will use JavaScript to interact with the HTML you create.

2.      The example in section 10.6 is very helpful for understanding a.) how to use JavaScript to read/write the value of an HTML input box b.) how to use JavaScript to make something happen when you click on a button.  Note though that this example is simulating individual craps games, which is different from what you are doing.

3.      Using Mozilla Firefox is highly recommended for testing.  However, your final result should run on Firefox and IE.

4.      You should not use document.writeln() inside your functions that are called by ‘onclick’.  Doing so erases your whole page and starts over with just the new content that you just output.  Instead if you need to output something to the user (or to debug) either use window.alert() or change the value of some input box.

.

Your web page must be constructed using Notepad or a similar text-only editor. The use of programs such as Microsoft Word, Microsoft Frontpage, DreamWeaver, ColdFusion, Mozilla Composer,. etc. will be considered an honor offense.

## Extra Credit

For a nominal amount of extra credit do some/all of the following:

1.      Make a new table that shows dynamically how your money changes over time.  For instance, have three boxes showing the amount of money you have under each of three scenarios and update these periodically (perhaps twice a second) to show how much money you have left after a certain amount of time.  To do this you will have to make some assumption about how many craps game / lottery tickets you do per year.

2.      Write a simulator for some casino game.  Not craps, because the book does that, but perhaps for poker or blackjack.  Show the cards that were played in some appropriate way. Have a button that causes one game to be played, or else have different buttons that lets the user choose how to play (e.g., hit or stand?  what to bet? double down?).  Update your total money appropriately when a game/hand is over.

## Deliverables

1. Your page should contain all of the elements described in the requirements section above.
2. All of your files should be in a folder called "Lab06" (without the quotes) on your web drive. Your instructor will assume that your web page is viewable at http://www.mXXXXXX.it.cs.usna.edu/Lab06/money.html where XXXXXX is your alpha number. You may want to check that this URL is viewable and that everything works correctly from a computer where somebody else is logged in. If you've goofed and linked to a file on your X drive, this will help you catch it!
3. Turn in the following hardcopy at the beginning of class on the due date, stapled together in the following order (coversheet on top):
1. A completed assignment coversheet.  Your comments will help us improve the course.
2. A printout of the source to your Lab06/money.html file.
3. If you use any external script files, include a printout of those too.