To complete this assignment you must --
The goal of this assignment is to allow you to become familiar
You'll be using variables, which are named memory locations.
You must always first declare a variable in memory by giving it a name with the command var namedvar1.
Furthermore, since the location is only reserved with that name and contains nothing at first, you must assign your variable a value before you use it with namedvar1 = somenumber.
Random numbers and Rounding
To generate a random number within some range, i.e. 0 to 100, you can multiply by that range: Math.random() * 100.
To round the number so it is no longer a decimal, use Math.round(), which rounds to the nearest integer (no decimal places).
Ex: Generate a random integer between 0 and 100: Math.round(Math.random() * 100));
- Open Notepad++ on your computer, found by clicking
- Using your text editor create a new document named "cop2500lab2.html".
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). A simple way to display output is using document.write(), which was
covered in the last lab session. 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.
Fahrenheit to Celsius Conversion
|INPUT:||var far1, far2, far3;|
|OTHER VARIABLES:||var 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
|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 annoying
this repetition is -
imagine doing 1,000,000.
|OUTPUT:||display cent1, cent2 and cent3 using document.write
||Observe that the algorithm doesn't
say anything about the display format.
You take care of that when
|OTHER VARIABLES:||var taxrate;
|Why do you think taxrate|
is not an input variable?
Could it be? Should it be?
|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
|OUTPUT:||display the total and subtotal using document.write;
||Note we aren't returning|
the tax rate or amount of tax.
Open a browser and verify that the file displays properly. In order to obtain full credit for the lab you need to email your source code to your grader.