In lab02 you learned some basics of JavaScript. We saw the concepts of
2+4
, 5*x
js>
), the JavaScript interpreter evaluates the expression (it does the math and gives you result)2+3*4
is 14
, not 20
.
x=5;
which makes x
have the value 5
x=5
, then:
x
is the name of the variable5
is the value of the variablenumber
is the type of the variable2+x
, +
is an operator, and 2
and x
are the operands) ()
after them), and properties (no () afterwards). For example:myName.toUpperCase();
the method is toUpperCase()
, and it is a method of the myName
object. myName.length
, the property is length
, and it is a property of the myName
object. In lab04, we saw
dayOfWeek()
)If you need a refresher on any of these concepts, review lab02 and lab04.
In this lab, we'll:
In this step, you'll create two new subdirectories:
~/public_html/cisc103/lab06 |
for web content we create in this lab |
~/cisc103/lab06 |
for files we create in this lab that do not go on the web |
You'll also copy some files:
copy all files from /www/htdocs/CIS/103/pconrad/07F/labs/lab06 |
to ~/public_html/cisc103/lab06 |
For this lab, I'll still "walk you through" the commands do to this—see steps 1a and 1b below.
From next week on, I'll just give you instructions similar to the ones given above, and you'll be expected to know the Unix commands to accomplish these tasks.
So this week, don't just type the commands, learn the commands.
You'll also be expected to be able to specify the Unix commands, exactly as you would type them at the Unix prompt to accomplish these tasks on the first midterm exam (and every subsequent exam in this course.)
question type | given this: | what you'll have to do: |
---|---|---|
short answer | a goal you want to accomplish | specify the unix command (or sequence of commands) just as you'd type it (them) at the Unix prompt, that accomplishes that goal |
short answer | a Unix command (or sequence of commands) | a plain english description of what the command (or sequence of commands) does |
multiple choice | a goal you want to accomplish | identify which of the unix commands (or sequences of commands) given will accomplish that goal. |
multiple choice | a goal you want to accomplish | given several unix commands (or sequences of commands), all of which will will accomplish that goal except for one, identify the one that will not work |
In this step, you'll create two new subdirectories:
~/public_html/cisc103/lab06 |
for web content we create in this lab |
~/cisc103/lab06 |
for files we create in this lab that do not go on the web |
The quickest way to do this is with the following Unix commands:
> mkdir -p ~/public_html/cisc103/lab06
> mkdir -p ~/cisc103/lab06 >
As a reminder (review from previous labs):
mkdir
is the Unix command to create a directory (a folder) -p
means "create all directories along the entire path if they don't exist—and if any or all do already exist, don't complain about it."~/
means "the home directory of the person currently logged in" Assuming that:
~/public_html/cisc103
and ~/cisc103
already exist:You could type the commands in the box below.
> cd public_html/cisc103
> mkdir lab06
> cd ../..
> pwd
/home/usra/d9/55560
> cd cisc103
> mkdir lab06
>
copy all files from /www/htdocs/CIS/103/pconrad/07F/labs/lab06 |
to ~/public_html/cisc103/lab06 |
To be able to do this successfully, you need to remember four Unix concepts:
cp
is the copy commandcp
always copies from somewhere, to somewhere.*
(the asterisk, or "star"), when it is part of a filename, means the wildcard—it stands for "all files".
(the period by itself) means "the current directory" It is also helpful to remember the following:
pwd
is the print working directory command—it tells us what directory we are in right nowcd
is used to change from one directory to anotherIn each case, we first change directory into the directory we want to copy the files into, and then we copy the files (using a wildcard) to .
(the "dot" that stands for current directory). See the box below for the commands.
Type these commands, but don't just type them.
As you type these commands, follow along on the map of directories and files with what you are doing. Make sure you understand every command and what's its purpose is. If you aren't sure, find out! Read the Unix textbook, ask a classmate, ask your TA, or come to the professor's office hours, use a search engine—there are many ways before the next exam!
> cd
> pwd
/home/usra/d9/55560
> cd public_html/cisc103/lab06
> cp /www/htdocs/CIS/103/pconrad/07F/labs/lab06/* . > ls ... this files you copied are listed here ...
The most straightforward way to copy the files is with the following two Unix commands. Note that since both of these commands use absolute pathnames, it does not matter what the current directory is—these commands will work from anywhere. (Be sure you know the difference between an absolute pathname and a relative pathname before the exam!)
> cp /www/htdocs/CIS/103/pconrad/07F/labs/lab06/* ~/public_html/cisc103/lab06
>
In lab04, we worked with a file containing a function definition, namely the file dayOfWeek.js.
That file makes a repeat appearance this week. In step 1, you copied that files into your ~/public_html/cisc103/lab06
directory.
Our next step is to create a web page that will use that file.
Use the proper Unix command to change your working directory to ~/public_html/cisc103/lab06
You should already have a file called lab06.html in your directory (from the files you copied in Step 1). Type the Unix command that brings up emacs so that you can make changes to this file.
The box below shows the contents of this file, with some lines in bold that you need to add. Add these lines at the appropriate spots.
Also, in bold, are some places where you should change the name Phill
or Phill Conrad
to your name.
<html> <!-- lab06.html Phill Conrad for CISC103, 07F, 10/02/2007 --> <!-- Illustrate <script> element in head for function definitions --> <!-- and <script> element in body for function calls -->
<head>
<title>CISC103 lab06 exercise</title>
<script type="text/javascript" src="dayOfWeek.js"></script>
</head>
<body> <h1>Welcome to Phill's Web Page</h1> <h2>Today is <script type="text/javascript">document.write(dayOfWeek());</script></h2> <p>Have a great day!</p> </body> </html>
Once you've made the changes, save the file.
As with several other steps in this lab, this is the last time I'll describe this step in detail. From this point forward, you should be able to manage the details of this step yourself—I'll just say: make the file readable on the web, or bring up this file in a web browser, and you'll be expected to know what to do.
To make this file readable, we can do this:
> chmod -R 755 ~/public_html/cisc103/lab06
>
Explanation:
chmod
part means change the permission modes-R
part stands for recursive, which in this case, means: apply the change to this directory, and all files and directories inside it, and all files and directories inside those directories, and so on until you "hit the bottom" 755
is the octal number that stands for rwxr-xr-x
(in binary: 111 101 101), which indicates the permissions needed for this fileFor more information on file permissions, see
For the exam, also be sure you know this technique (and it is handy in practice too).
If you want to make the current directory, and all the files in and under it readable, you can type this:
> chmod -R 755 .
>
You should know by now where to look on the web for a file called ~/public_html/cisc103/lab06/lab06.html.
I should not have to tell you.
But, one last time, I'll tell you where to look. From next week, you are on your own here also! (And you need to know for the exam)
Look at http://copland.udel.edu/~youruserid/cisc103/lab06/lab06.html
You should see that the greeting produced by the line:
<h2>Today is <script type="text/javascript">document.write(dayOfWeek());</script></h2>
shows up your your web page like this:
Note that the word for the actual day of the week does not appear in your HTML file. Instead, it is generated by the JavaScript!
Tuesday
or Thursday
or whatever, we have a <script>
element.<script>
element, there is function call, namely dayOfWeek()
. dayOfWeek()
function call is passed as the actual parameter to another function call
document.write()
write()
is a method of the document
objectThe <script>
element nested inside the <h2>
element—which in turn is nested inside the <body>
element, makes a function call to dayOfWeek()
. This actually calculates the day of the week, and puts it on the web page.
However, you cannot have a function call without a function definition.
The function definition is inside the file dayOfWeek.js
. This file contains all the details that tell JavaScript how to access today's day of the week.
Inside the <head>
element, we have a <script>
element that pulls this function definition into our web page. We write this, and put it inside the <head>
element:
<script type="text/javascript" src="dayOfWeek.js"></script>
But, we could also have written this, and put it inside the <head>
element:
<script type="text/javascript"> // function dayOfWeek // consumes: nothing // produces: a string representing the current day of the week (e.g. "Tuesday") function dayOfWeek() { // set up an array with all the days of the week, spelled out var daysOfWeek= ["Sunday","Monday","Tuesday","Wednesday", "Thursday","Friday","Saturday"]; // create an object representing today's date var today = new Date(); // use the "getDay()" method to get the index of today's date var index = today.getDay(); // returns 0 for Sunday, 1 for Monday, etc. // return that element of the array return daysOfWeek[index]; } </script>
There are several—here are just a few.
js>
prompt and typing first load("dayOfWeek.js");
then dayOfWeek();
—in fact that is exactly what we did near the end of lab04! Yes! Because the file dayOfWeek.js contains the function definition for dayOfWeek()
, you can call that function from any web page that has access to that function defintions.
To make the "current day of the week" show up on a web page, do just what you did in Step 2 above:
<script>
element into the head that pulls in the function definition, like this: <script type="text/javascript" src="dayOfWeek.js"></script>
<script>
element anywhere inside the body—inside an <h2>
or an <h1>
, or a <p>
, or any element you like—exactly where you want the day to show up, and inside that <script>
element, put the function call inside a document.write()
method call, like this: <h2>Today is <script type="text/javascript">document.write(dayOfWeek());</script></h2>
Now that we see how useful a function definition can be, we are going to write another one. That's what we'll do in Step 3 of this week's lab:
As you can see, JavaScript functions can be very useful. Once you have a function definition you can do interesting things with it—first at the js> prompt, and later inside a web page.
In the rest of this lab, we are going to work with some function definitions just at the js>
prompt. We'll incorporate those definitions into a web pages in later labs.
Now change your working directory to your ~/cisc103/lab06
directory—not the one under public_html
, but the other one.
I'm not giving you the command—on purpose! You need to come up with it on your own. By now, that should not be a problem.
After you type your cd command, use a pwd command to be sure you are in the right place.
We are going to write a JavaScript function to solve the following problem:
Determine how much pizza (in square inches) you get if you order two regular pizzas (12 inch round) vs. 1 x-large pizza (16 inch round).
Understand the problem statement before continuing.
What we need to do is to calculate the "area" of two regular pizzas (both circles) and one large pizza (also a circle). So, we are going to use the formula for area of a circle, which is area = πr2 , where r is the radius of the circle.
However, area of a circle is based on the radius, and pizza is normally described by the diameter—a 12-inch pizza has a diameter of 12 inches, not a radius of 12 inches. So, we need to divide the diameter in half before calculating the area.
So, we will write the definition of a JavaScript function called areaOfPizza() that will
That takes us to our next step.
To create our function definition, we will edit a file called areaOfPizza.js
Use emacs to open up areaOfPizza.js
for editing.
Inside the file, type the JavaScript code shown below.
// areaOfPizza.js compute area of pizza, given diameter
// P.Conrad for CISC103 10/02/2007
// consumes:
// diameter: number, diameter of the pizza in inches
// produces:
// area: number, area of the pizza in square inches
//
// examples
// js> areaOfPizza(12);
// 113.10
// js> areaOfPizza(16);
// 201.06 function areaOfPizza(diameter) { radius = diameter / 2; area = Math.PI * radius * radius; return area; } // end function areaOfPizza
Then, save the file, and continue with the next step
Inside the code, there are several lines that start with //
. These lines are "comments".
A few of the comments contain examples of using the function—what you type at the js>
prompt, and what is supposed to come back. We can use these examples to test the function.
Bring up the JavaScript prompt by typing js at the Unix prompt (if you completed lab04, that should work—if not, go back to lab04 and get the long version of the command to access the JavaScript prompt.)
At the JavaScript prompt, type the command to load areaOfPizza.js into the JavaScript interpreter:
js> load ("areaOfPizza.js");
js>
Then, type the following (taken from the comments inside the file). See if the output you get agrees with that shown below:
js> areaOfPizza(12)
113.09733552923255
js> areaOfPizza(16)
201.06192982974676
js>
You will note that the answers given are approximately equal to the answers in the examples from the comments. That is because the answers in the comments were calculated by hand, while the answers from JavaScript are much more precise. However, these answers are close enough that we consider that the script appears to be working!
Type the command that takes you out of JavaScript. If you don't remember it, look back at lab04.
Following the example from step 11 of lab04, create a script of your work.
Call your script lab06.txt. You should be in the directory ~/cisc103/lab06
before you type the command script lab06.txt
In your script:
pwd
command to show you are in ~/cisc103/lab06
ls -l
command to list your files cat areaOfPizza.js
to list the contents of that filejs
to go into JavaScriptareaOfPizza()
correctly calculates the value for a 12 inch and a 16 inch pizzaexit
to end the script Upload both the file areaOfPizza.js and the file lab06.txt to WebCT, and submit.
Remember that you will have to transfer these files to your hard drive before you can submit them (refer back to Step 12 of lab04 if you are not clear on how to do this.)