PATH: Instructional Server> COP 1830> Projects>

COP 1830 Project 5 - JavaScript Functions


Purpose:

This project is intended to give students experience in composing a small web page involving JavaScript functions. The project will use a web-based form to gather input data and display results. Although many different techniques exist for coding and executing scripts, this project will require that the script be coded as a JavaScript function in the head section of the source code and executed via a form button. This technique differs from the one used Project 4 that relied primarily on the use of "prompt dialog" boxes for input and HTML output using JavaScript write methods. The necessary information about how to pass data between a form and a JavaScript function will be provided below. As with prior projects, this project will be written using a plain text editor to help students develop a fundamental understanding of XHTML and JavaScript syntax. Keep the project as simple as possible with respect to format, but attempt to produce a page as close as possible to the one in the illustration below.

Sample Output:

Project 5 Web Page Illustration

Look at Chapter 17 in your HTML textbook and chapters A, B and E in your JavaScript Virtual Textbook and review the information about how to produce web-based forms with XHTML. Name the four input textboxes (in order of appearance): stickerPrice, downPayment, interestRate, and termMonths. Notice the "camel style" capitalization used in the names and remember that JavaScript is case-sensitive. Name the two output textboxes (in order of appearance): totalCost and monthlyPayment. Name the JavaScipt function that you write in the head section of your source code calculateCost, and provide a "parameter" name of formData following the function name in parentheses. The function will be executed via a method recognized by form buttons that is named onClick. Inside the form in the body section of your page, write the source code for your [Calculate] button (see illustration above) as follows:

	<input type="button" name="calculateButton" value="Calculate"
	onclick="calculateCost (this.form)" />

The onClick attribute in the statement above will "call" (activate) the calculateCost function in the head section and pass information about all form elements to the function using the standard identifier this.form. The function will then have access to all properties of the form. To refer to the value of a form element, use the following syntax:

    formData.elementName.value

where the labels are interpreted as follows:

For example, to refer to the sticker price of the car in this project, you would type:

    formData.stickerPrice.value

Using the syntax above, you can either (1) use the value of any element that was input on a form or (2) change the value of any form element (for output display).

Variable List:

Your JavaScript function will declare its own variable labels to use to accomplish its task. The following "variable list" defines the labels you should use within your function.

LABEL DESCRIPTION SOURCE USAGE DESTINATION
PRICE Sticker price on the car Form element
stickerPrice
for PRIN & TCOST ---
DOWN Down payment Form element
downPayment
for PRIN ---
RATE Annual interest rate Form element
interestRate
for INT ---
TERM Term of loan in months Form element
termMonths
for INT & MPAY ---
PRIN Principal on the loan Calculated for INT & TCOST & MPAY ---
INT Interest on the loan Calculated for TCOST & MPAY ---
TCOST Total cost including interest Calculated --- Form element
totalCost
MPAY Monthly payment Calculated --- Form element
monthlyPayment

Algorithm:

You are not required to analyze the steps necessary to accomplish the objective of the script. That work has already been done for you, resulting in an "algorithm" (logical recipe) as follows.

A. Start - receiving the entire form structure in an identifier labeled "formData".
B. Store data extracted from the first four textbox elements on the form.
   B1. Parse PRICE from the textbox named stickerPrice.
   B2. Parse DOWN from the textbox named downPayment.
   B3. Parse RATE from the textbox named interestRate.
   B4. Parse TERM from the textbox named termMonths.
C. Calculate and store interim and final answers.
   C1. PRIN is PRICE minus DOWN.
   C2. INT is PRIN times (RATE divided by 100) times TERM divided by 12.
   C3. TCOST is PRICE plus INT.
   C4. MPAY is (PRIN plus INT) divided by TERM.
D. Return results to the appropriate form elements (textboxes).
   D1. Assign the value in the textbox named totalCost to be the value in TCOST.
   D2. Assign the value in the textbox named monthlyPayment to be the value in MPAY.
E. End.

Example:

An example of a web page that is similar in approach to the one that you will be writing is available on my web site at:

     http://www.gibsonr.com/classes/cop1830/jsexample.html

You can use it as a guide when preparing your page.

Project Constraints and Objectives:

Tips:

Submission:

Login to the and use the Project 5 drop box under the Lessons tab to attach your "project5.htm" file.

PATH: Instructional Server> COP 1830> Projects>