# COP2500 Assignment 2

Deliverables: To complete this assignment you must :
1.) Write the HTML and JavaScript page described below and email your work (the notepad document) to the lab instructor (Mikel)

Introduction: The goal of this assignment is to allow you to become familiar with converting algorithms to JavaScript and to become familiar with a few basic JavaScript commands.

Procedures:
PART 1:

1. Open a text editor on your computer. You will want to use either Notepad. Notepad should be found by clicking Start/Programs/Accessories.
2. Using your text editor create a new document named "cop2500lab2.html".
3. Convert the following two algorithms into JavaScript code within your HTML document. Your final web page should display the algorithm title, the input and the output. The specific format you use to display the results is up to you, but it should be clear what the input and output means (in other words, don't just list rows of numbers without words or labels describing them). The first algorithm, Fahr2Cent(), converts three temperatures given in Fahrenheit to Centigrade. The second algorithm, PriceCalc(), computes sales tax and total price for a product.

Fahr2Cent()
INPUT: Number far1, far2, far3;
OTHER VARIABLES: Number cent1, cent2, cent3; We'll use these variables
to store the results.
INITIALIZATION: far1 = 32;
far2 = 78; far3 = 212;
Storing initial values
for the Fahrenheit temps
in memory.
COMPUTATION: cent1 = 5 * (far1 - 32) / 9;
cent2 = 5 * (far2 - 32) / 9;
cent3 = 5 * (far3 - 32) / 9;
Converting F - C using
the standard formula.
Notice how innefficient
this repetition is -
imagine doing 1,000,000.
OUTPUT (print) cent1, cent2, cent3 Notice the algorithm doesn't
You take care of that when
You write the JavaSCript.

PriceCalc()
INPUT: Number subtotal;
OTHER VARIABLES: Number taxrate;
Number salestax;
Number total;

INITIALIZATION: taxrate = 0.06;
subtotal = 57.24;
COMPUTATION: salestax = subtotal * taxrate;
salestax = Math.round(salestax * 100 );
salestax = salestax / 100;
total = subtotal + salestax;
If you are unsure of what the
middle two lines are doing,
try leaving them out and
running the code. Math.round()
is one of hundreds of functions
JavaScript provides to you.
OUTPUT (print) : subtotal, total Note we aren't returning
the tax rate or amount of tax.
4. Open a browser and verify that the file displays properly. Email your source code to mikel @ {cs.ucf.edu}

## Here is a brief list of helpful HTML tags and JavaScript functions.

(You do not need all of these to complete the lab)

• Some HTML tags:

<html> </html> : tags that start and end each document (required).

<title> </title> : tags that start and end the document title (required).

<body> </body> : tags that start and end the visible portion of your web page (required).

<script language="JavaScript"> </script> : tags that start and end your JavaScript code.

<b> </b> : tags that start and end bold font.

<i> </i> : tags that start and end italic font.

<center> </center> : tags that start and end centered text.

<h1> </h1> : tags that start and end standard font sizes (h1 - h7).

<br> : line break.

<p> : new paragraph.

<hr> : horizontal rule.

Some JavaScript Functions

document.write(); JavaScript standard output function.

Math.round(): JavaScript standard rounding function.

This section provides a template to convert pseudo-code IF-THEN-ELSE blocks and DO-LOOPS to JavaScript:

Pseudo-Code JavaScript
IF (predicate) THEN
Basic block if predicate is true.
ELSE
Basic block if predicate is false.
ENDIF
if (predicate)
{ Basic block if predicate is true. }
else
{ Basic block if predicate is false. }
FOR i = 0 to 9 DO
loop body.
DOEND
for (i = 0; i < 10; i++)
{ Loop body. }