PATH: Instructional Server> COP 1830> Projects>

COP 1830 Project 4 - JavaScript


Project Purpose:

This project is intended to give students experience in composing a small web page involving JavaScript. Although many different techniques exist for coding and executing scripts, this project will require that the script be coded in the head section of an XHTML file. This program will rely primarily on the use of "[pop-up prompt (dialog) boxes]" for input and will produce HTML output using JavaScript document.write methods. 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.

Program (Script) Objective:

Request and store two numbers entered by the user via dialog boxes similar to those shown in the INPUT area of the Sample Output below. Then calculate their sum, difference, product, and quotient. Then display those results in the browser window in a manner similar to that shown in the OUTPUT area of the Sample Output below.

Sample Output:

The INPUT area of the illustration below shows two prompt dialog boxes that your JavaScript will have to produce in sequence to allow the user to enter two input values. These boxes can be executed via a method named prompt. See the W3Schools tutorial page about JavaScript Popup Boxes. The OUTPUT area of the illustration shows how your browser window should appear when the program is complete.

Project 4 Illustration

Variable List:

Your JavaScript code will declare its own variable labels to use to accomplish its task. The following "variable list" defines the labels you should use within your code. Notice the "camel (hump)" naming convention used on the first two variable identifiers, that uses capitalization in place of blank spaces.

LABEL DESCRIPTION SOURCE USAGE DESTINATION
firstString First digit string enter by the user prompt dialog box for first Browser Document
secondString Second digit string enter by the user prompt dialog box for second Browser Document
first First converted entry value assigned from
parsed firstString
for sum, diff,
product & quotient
---
second Second converted entry value assigned from
parsed secondString
for sum, diff,
product & quotient
---
sum Sum of first plus second Calculated --- Browser Document
diff Difference of first minus second Calculated --- Browser Document
product Product of first times second Calculated --- Browser Document
quotient Quotient of first divided by second Calculated --- Browser Document

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 an an "algorithm" (logical recipe) as follows.

A. Start.
B. Store data acquired from the two prompt dialog boxes.
   B1. firstString from a prompt dialog box.
   B2. secondString from a prompt dialog box.
   B3. Parse firstString as a floating point number and assign it to variable first.
   B4. Parse secondString as a floating point number and assign it to variable second.
C. Calculate and store results.
   C1. sum as first plus second.
   C2. diff as first minus second.
   C3. product as first times second.
   C4. quotient as first divided by second.
D. Display results in the browser's document window as seen in the Sample OUTPUT.
   D1. Page Heading (H3).
   D2. A paragraph element showing the two string values entered.
       D2a. firstString
       D2b. secondString
   D3. A paragraph element showing the results.
       D3a. sum.
       D3b. diff.
       D3c. product.
       D3d. quotient.
E. Display blank line and wrap-up message as seen in the Sample OUTPUT.
F. End.

Note that steps B-D should be handled by JavaScript code in the head section of the web page, but step E should be performed as part of the body section of the web page. Read the source code for the JavaScript example file entitled "Entirely in the head section" posted on my web site at:

http://www.gibsonr.com/classes/internet/examples/javascript/jsex_all_in_head.htm

In order to execute the JavaScript content of that document, you might have to reconfigure your browser's security settings as described on the page about Browser Security Configuration for Scripts.

Web document content generated by script instructions in the head section (as shown in the example referenced above) will precede document content contained in the body section of the XHTML file.

Project Constraints and Objectives:

Tips:

Submission:

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

PATH: Instructional Server> COP 1830> Projects>