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.

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:
formData is a user-defined name given to the entire structure of form data
passed into the function by the onClick method in the Calculate button.elementName should be replaced by the name of the form element (in this
project each textbox) that you want to access.value literally refers to the "value" property of the element.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).
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 |
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.
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.
txt to the end of the filename.<!-- Comment
here --><?xml version='1.0' encoding='utf-8'?> <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> <html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
// style of JavaScript comment notation at the front of those statements to
"comment out" the alert statements prior to submitting your code. For example: //alert ("Input section of script reached.");<!-- and --> in XHTML and /* and */
in JavaScript) to mask any code that is causing trouble, move on to a different element,
and come back to solve the problem later.
Some frustration can be a valuable motivator to reinforce lessons.
But too much can inhibit learning.
Schedule your time to allow some breaks between working sessions.Login to the and use the Project 5 drop box under the Lessons tab to attach your "project5.htm" file.