Summer 2007
week 3: 5/25
Hello everybody,
Welcome to the blog for DMA106, JavaScript, to those of you who haven't visited yet. Although this blog is meant to serve as a supplement to the class, and it definitely can go a long way to keeping you caught up if you miss a class here or there, as I will repeat here almost everything I cover in class, it is by no means a replacement. Please remember, your grade will suffer seriously if you do not attend class.
I think we did a lot of nice work this past Friday, and I was quite happy with how you all seem to be picking up the material. Not that it is easy by any means for most of you, but I can see you getting it little by little. You'll notice by now that a little bit of persistence, and a lot of tedious attention to detail really pay off.
Also, if you cannot get something in the homework on your own, please find someone (especially me) to help you during the week before your homework is due. As I mentioned to everyone, if you spend some time on a particular piece of homework and cannot get it to work, move on to the next thing is there is something else to do. That way I will be able to see that you tried all of it. If you do not make an attempt, then I cannot see where your mistakes are to help you.
Hope you've all had a nice long weekend. See you Friday.
Good Luck, Carter-
Welcome to the blog for DMA106, JavaScript, to those of you who haven't visited yet. Although this blog is meant to serve as a supplement to the class, and it definitely can go a long way to keeping you caught up if you miss a class here or there, as I will repeat here almost everything I cover in class, it is by no means a replacement. Please remember, your grade will suffer seriously if you do not attend class.
I think we did a lot of nice work this past Friday, and I was quite happy with how you all seem to be picking up the material. Not that it is easy by any means for most of you, but I can see you getting it little by little. You'll notice by now that a little bit of persistence, and a lot of tedious attention to detail really pay off.
Also, if you cannot get something in the homework on your own, please find someone (especially me) to help you during the week before your homework is due. As I mentioned to everyone, if you spend some time on a particular piece of homework and cannot get it to work, move on to the next thing is there is something else to do. That way I will be able to see that you tried all of it. If you do not make an attempt, then I cannot see where your mistakes are to help you.
Hope you've all had a nice long weekend. See you Friday.
Good Luck, Carter-
- TOPICS:
- Operators:
- Operations:
- LINK User-defined Functions:
- New Built-in Functions:
- Operators:
- HOMEWORK:
- From the book—Using Notepad, Textpad, or TextEdit (Mac), please type an .html document that contains the following questions and answers from the book (please include all necessary scripts, and please try to distinguish the questions from the answers):
- Chapter 4: 1, 2 and 3;
- Chapter 5: 1, 3 - 5, and 7;
- Chapter 4: 1, 2 and 3;
- Type the User-Defined Function below:
- Declare a new variable with the name userName.
- Initialize the variable with a prompt() function that requests the user's name.
- Next, declare another variable named choice, and initialize it with a confirm() function that tells the user to click okay if they want a greeting displayed in an alert() function or a document.write() function.
for example:
"OKAY: --> alert();\nCANCEL: --> document.write()"
**NOTE**
the two characters, \n, function like a <br/> tag within alert(), confirm(), and prompt() functions.
- Now, declare a third variable named display, and initialize it with a conditional operation.
- The first operand of the conditional operation should be the choice variable, followed by a question mark.
- The second operand of the conditional operation should be the string: "alert('hello, '+ userName)".
- The third operand , and final, of the conditional operation should be the string: "document.write('hello, '+ userName)".
- Finally, in the next line, type the eval() function with the variable, display between the parentheses.
- Declare a new variable with the name userName.
- INTRODUCE:
- Operators: In the last class, you were introduced to the elements in JavaScript known as operators. Below, you will find listed many of the ones we will consider in class this term:
- Numerical Operators:
- Addition ( + ) —Combines two quantities (numbers) to form a sum.
- Subtraction ( - )—Reduces one quantity by another quantity.
- Multiplication ( * ) —The process by which one quantity is added to itself a given number of times: also ×.
- Division ( / ) —The process of determining precisely how many times one quantity is contained in, or may be divided by, another quantity: also ÷.
- Incrementation ( ++ ) —The process of increasing a quantity by one.
- Decrementation ( -- )—The process of decareasing a quantity by one.
- Subtraction ( - )—Reduces one quantity by another quantity.
- Addition ( + ) —Combines two quantities (numbers) to form a sum.
- Comparison Operators:
- Less Than ( < ) —Compares two quantities (numbers) based on whether one is smaller than the other or not.
- Greater Than ( > )—Compares two quantities (numbers) based on whether one is larger than the other or not.
- Less Than or Equal To ( <= ) —Compares two quantities (numbers) based on whether one is smaller than the other or whether they are the same.
- Greater Than or Equal to ( >= ) —Compares two quantities (numbers) based on whether one is larger than the other or whether they are the same.
- Not Less Than ( !< ) —Compares two quantities (numbers) based on whether one is NOT smaller than the other.
- Not Greater Than ( !> ) —Compares two quantities (numbers) based on whether one is NOT larger than the other.
- Equal To ( == ) —Compares two quantities (numbers) based on whether they are the same or not.
- Not Equal To ( != ) —Compares two quantities (numbers) based on whether they are NOT the same.
- Greater Than ( > )—Compares two quantities (numbers) based on whether one is larger than the other or not.
- Less Than ( < ) —Compares two quantities (numbers) based on whether one is smaller than the other or not.
- Logical Operators:
- And ( && ) —An element used to combine two operations or solutions as one compound operation or one compound solution.
- Or ( || ) —An element used to present an option between two or more solutions or operations.
- Not ( ! ) —An element which eliminates the possibility of a particular solution or operation.
- Or ( || ) —An element used to present an option between two or more solutions or operations.
- And ( && ) —An element used to combine two operations or solutions as one compound operation or one compound solution.
- Assignment Operators:
- Assignment ( = ) —An element used to provide a new value to another element such as a variable or array.
- Assignment-Addition ( += ) —An operator used to increase the current value of an element by a given number and assign the element the new value.
- Assignment-Subtraction ( -= ) —An operator used to decrease the current value of an element by a given number and assign the element the new value.
- Assignment-Addition ( += ) —An operator used to increase the current value of an element by a given number and assign the element the new value.
- Assignment ( = ) —An element used to provide a new value to another element such as a variable or array.
- Numerical Operators:
- Operations—these are actions taken upon elements of data, on numbers and strings for example, by operators that change or alter their values in some way.
- Numerical Operations—This group of operations are perhaps the most intuitively to our understanding than most of the others. Save one or two of them, they need no real explanation; therefore, below you will see examples of various numerical operations:
o p e r a t i o n
5 − 1
operand 1 operator operand 2
3 × 8
operand 1 operator operand 2
4 ÷ 2
operand 1 operator operand 2
7 % 2
operand 1 operator operand 2
6 + 9
o p e r a t i o n
- Comparison Operations—This group of operations are probably also recognizable at first glance; however, the results of these operations are probably less than clear, no pun intended. If we are to understand operations in the way they are defined above, as actions upon elements of data resulting in another element of data, then maybe it can be made more clear. In this case, the resulting data is of a completely different type. If what is being compared by the operators are two numerical values (two elements of numerical data), then the result is actually an element of boolean data. What results from all of the operations below, is simply a true or false:
o p e r a t i o n
5 < 1
operand 1 operator operand 2
3 > 8
operand 1 operator operand 2
4 ÷ 2
operand 1 operator operand 2
7 <= 2
operand 1 operator operand 2
6 >= 9
operand 1 operator operand 2
3 !< 3
operand 1 operator operand 2
8 !> 9
operand 1 operator operand 2
6 == 6
operand 1 operator operand 2
4 != 4
o p e r a t i o n
- Conditional Operations—These operations are arguably the most NON-intuitive element within the whole of the JavaScript scripting language. The reason I say this is that there is nothing in our prior education that might suggest to us how to interpret them. Without having the structure explained to us first, an example of one of these operations would probably remain inscrutable. That being said, once described and demonstrated, they are rather simple to use. Much like learning new words, it just takes a bit of time for us to learn their meanings and then to get used to employing them ourselves.
First, unlike all other operations we have considered such as the examples above, this is the only type with three (3) operands. Moreover, they do not employ any of the operands discussed so far in class. Their structure follows the following pattern:
conditional operation structure
operand 1 ? operand 2 : operand 3 ;
conditional operation
example: (5 < 6) ? "doremifa" : "solatido" ;
Now, let us see if we can understand what's going on with this operation: in this kind of operation, the computer only evaluates the first operand. But what does this mean? What does it mean to evaluate? In this case, the computer will only consider, process, or calculate the first operand. The computer will not evaluate in any depth the second or third operands. Their meaning or values is of no consequence to the function of this operation. However, the value of the first operand, is of great consequence. The computer will evaluate the first operand and attempt to determine whether it equals, or is equivalent to, or evaluates to true or false.
To simplify matters, in my example above, I have only made the first operand meaningful. The second and third operands are just seemingly random strings of characters. The computer wouldn't be able to make real sense of these two anymore than we could. The first one, on the other hand, will either be true or false.
In this case, five is indeed less than six, so this little expression—the first operand—will evaluate to true, and not false. But what does this mean? What happens after that?
In this sort of operation, if the first operand evaluates to TRUE then the result of the operation is operand 2; however, if the first operand evaluates to FALSE then the result of the operation is operand 3. Therefore, in the above example, since 5 is less than 6, it is the same as true: the first operand then is true. The result of this conditional operation is therefore operand 2, which is equal to the string, "doremifa".
- User-defined Functions—In JavaScript, a function is an action that is performed or taken by JavaScript. Another name for a function is the term method as there is no difference between the two elements. For reasons that will be explained at a later time, the terms are often used interchangeably. Up to this point already in the first three weeks of class, we have identified a number of built-in functions that help to form the basis of what JavaScript does as a scripting language. Each of these functions performs an action: alert() , confirm() , prompt() , parseInt() , and typeof() . Now, whether we use these functions that have already been defined by the language itself, or whether we as users and programmers create and define our own functions makes little difference. A function is always visually identified by the pair of opening and closing parentheses that follows its name, and it always performs an action or series of actions. A user-defined function is no different. Here are the four (4) elements required for creating a new user-defined function:
- the function keyword
- a unique name that is not one of the reserved JavaScript keywords;
- a pair of parentheses to enclose what we will later learn are called parameters, or arguments;
- a pair of curly brackets that delimits the extents of the code to be included in the function.
<script language="javascript" type="text/javascript">
<!--- function compOp()
- {
- var userNum = prompt("Please type
- a number.","");
- var temp = parseInt(userNum);
- var ans = (temp % 2)? "ODD" : "EVEN";
- alert("Your number, " + temp +
- ", is "+ans);
- }
//-- >
</script>
Now, let us go over this script line-by-line so that we can determine what is happening.
- the function keyword;
- the unique name of the function: compOp;
- the opening and closing parentheses; and
- the opening, line 2, and closing line 9, curly brackets for the entire function.
- the var keyword;
- the unique name of the variable: userNum;
- the assignment operator ( = ); and
- the prompt() function which requests data from the user.
- the var keyword;
- the unique name of the variable: temp;
- the assignment operator; and
- the value assigned, which is a JavaScript built-in function, parseInt(), which converts string data to numerical data. Recall, that anything that the user types into the prompt() function will, strictly speaking, be a string. Therefore, in order for any numerical operations are to be performed on this user's number, it must first be converted from a line of characters (string data) to numerals (numerical data) Without this, the remainder of the script will not function properly.
- the var keyword;
- the unique name of the variable: ans;
- the assignment operator; and
- the value assigned, which comes from a conditional operation. The first operand uses the modulo operator which provides the remainder when the number in temp is divided by 2. If the number is odd then the remainder will be 1. If the number is even, however, then the remainder will be 0. This is a lucky coincidence, because it just so happens that 0 & 1 are the numerical equivalents of FALSE and TRUE. Morover, they are what the conditional operation works with. If the remainder of operand1 is 1, then operand2, "ODD", will be the result of this operation; however, if the remainder of operand1 is 0 then operand3, "EVEN", will be the result. Try it and see if it works for you. Then, try switching the places of "ODD" and "EVEN" to see what happens.
Whatever the result of the operation, it gets assigned to the variable, ans. - The first term of the concatenated statement is the string—"Your number, ".
- The second term of the concatenated statement is the variable, temp, which contains the user's number entered in at the prompt at the beginning of the script.
- The third term of the concatenation is a very short string, ", is ".
- And the fourth and final term of the concatenation another variable, ans, which contains the result of the comparison operation, either the string "ODD" or the string "EVEN".
Line 1:
function compOp()
Here is the simple function declaration including:
Lines 3-4:
var userNum = prompt("Please type a number.","");
The first variable declaration and its initialization including:
Line 5:
var temp = parseInt(userNum);
The second variable declaration and its initialization including:
Line 6:
var ans = (temp % 2)? "ODD" : "EVEN";
The third variable declaration and its initialization including:
Line 7-8:
alert("Your number, " + temp + ", is "+ans);
This line is a simple alert() function which concatenates a statement together so that the user can see whether his number is ODD or EVEN.
- New Built-in Functions—Below, find a couple of new Built-in Functions explained. Although we have not yet used either of these in class, they have come up in the reading, and I will be mentioning them from time to time. Please review and make sure you understand. A couple of quick experiments should do the trick to help you comprehend their place in a script.
- typeof()—This function works with a piece of data and identifies what its data type is, whether it is numerical, string, or boolean, for example:
var myData = 1000;
var myType = typeof(myData);
alert(myType);
Above, we have two variables. The first one has 1000 as its numerical value. The second one is given a value by the typeof() function. Between the parentheses, you should notice that we have the other variable myData. This variable contains an element of numerical data, so the value of the variable, myType, will be numerical. Therefore, what you will see in the alert() box, is simply the word numerical.
- eval()—This function is similar to the parseInt() function in that it converts data from one form to another. In this case, it converts string data not into numerical data, but rather into JavaScript code itself. For example. it could convert the string "alert('hello, how are you?')" or even something as simple as "10 + 10" into a line of JavaScript code. Try a simple comparison between two alerts. They are typed like so:
var myString = "10 + 10";
var convertedString = eval(myString);
alert(myString);
alert(convertedString);
- Numerical Operations—This group of operations are perhaps the most intuitively to our understanding than most of the others. Save one or two of them, they need no real explanation; therefore, below you will see examples of various numerical operations:
- From the book—Using Notepad, Textpad, or TextEdit (Mac), please type an .html document that contains the following questions and answers from the book (please include all necessary scripts, and please try to distinguish the questions from the answers):
0 Comments:
Post a Comment
<< Home