You are on page 1of 3

SH1804

Using the Math Object for Practical Applications


Objects

Objectives:
At the end of the exercise, the students should be able to:

 Write a simple programming output in JS that incorporates objects and other JS


programming concepts and/or techniques.

Materials:

 Flash drive
 Desktop computer
 Basic text editor application (Notepad, Notepad++, or equivalent)
 Web browser (Chrome, Firefox, or equivalent)
 Internet connection (if applicable)

Procedures:

Activity 1 Creating an Internal JS Script

1. Open your basic text editing program and write a simple HTML document containing a
<SCRIPT> tag pair. This will serve as the code body for internal JS script.

2. Save your program as LabExer5-XXX.html (replace XXX with your initials, for example
LabExer5-ABC) in order for the file to be recognized as an HTML document. Give special
attention to the file extension (.html).
3. Create a copy of this file for use on the succeeding activities. When you’re finished, have
your instructor check your copies before you proceed to the next activity.

05 Laboratory Exercise 1 *Property of STI


Page 1 of 3
SH1804

Activity 2 Laying the Foundation

1. Using the HMTL file from the previous activity, write in the <SCRIPT> tag pair a
declaration of a variable named n that contains a prompt box function and an alert box
function:

<SCRIPT>
var n = prompt("Enter an integer number corresponding to
a circle's radius in inches:", "");
alert("The circumference of a circle having " + n + "
inches is " + answer);
</SCRIPT>

The prompt box function will allow the web page to ask a user to supply a number
corresponding to a radius of a perceived circle.

The alert box function will then display a calculated value for the circumference of the
circle.

Take note that this code will NOT produce a correct output yet; the corresponding
statement that will complete the script will be supplied in the next activity.

2. Have your instructor check your work for this activity. You may save a copy of this
activity for your own reference and study. The output for this activity is also important
for the next activity, so DO NOT proceed with the next activity without having this
activity checked. Skipping this activity may result in getting less points for the exercise.

Activity 3 Using the Math.PI Property

1. Using the HTML file from the previous activity, add a variable named answer that
contains the formula for computing the circumference of a circle in between the
prompt box function and the alert box function. You MUST use the Math.PI property
of the Math object for this step.

The completed code block for the script should look like this:

<SCRIPT>
var n = prompt("Enter an integer number corresponding to
a circle's radius in inches:", "");
var answer = 2 * Math.PI * n;
alert("The circumference of a circle having " + n + "
inches is " + answer);
</SCRIPT>

3. Open the HTML file for this activity and have your instructor check your work. You
may save a copy of this activity for your own reference and study. The output for this
activity is also important for the next activity.

05 Laboratory Exercise 1 *Property of STI


Page 2 of 3
SH1804

Bonus Activity Personalizing Your Work

1. Using a copy of the HTML file from the previous activity, personalize your exercise
output by editing the code to produce one (1) script that contains the following
mathematical formulae that use the value of Pi (π):

a. The area of a circle with a given radius (radius represented by a variable, such as r).
Use the formula πr2 (r in the formula is the variable for the radius).
b. The surface area of a sphere with a given radius (radius represented by a variable,
such as r). Use the formula 4πr2 (r in the formula is the variable for the radius).

2. Show your updated version of the laboratory exercise to your instructor; this will allow
for additional points for the entire laboratory exercise.

05 Laboratory Exercise 1 *Property of STI


Page 3 of 3

You might also like