Sunday, February 04, 2007

Spring 2007

week 3: 2/02

Hello everybody,
Welcome to the blog for DMA106, JavaScript, to those of you who haven't visited yet. By now, hopefully, everyone who is going to be in this class this term has made it to class at least once. If you haven't, then you need to make sure you speak to me this week sometime (2/5 - 2/9). We have already met for three classes, and if you haven't been yet then you are seriously behind. 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.

Good Luck, Carter-

  1. TOPICS:
    • Operators:
      • LINK   Numerical Operators,
      • LINK   Comparison Operators,
      • LINK   Logical Operators,
      • LINK   Assignment Operators
    • Operations:
      • LINK   Numerical Operations,
      • LINK   Comparison Operations,
      • LINK   Conditional Operations
    • LINK   User-defined Functions:
    • New Built-in Functions:


  2. HOMEWORK:
    1. 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):
      1. Chapter 4: 1, 2 and 3;
      2. Chapter 5: 1, 3 - 5, and 7;


  3. INTRODUCE:
    1. 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:
      1. Numerical Operators:
        1. Addition ( + ) —Combines two quantities (numbers) to form a sum.
        2. Subtraction ( - )—Reduces one quantity by another quantity.
        3. Multiplication ( * ) —The process by which one quantity is added to itself a given number of times: also ×.
        4. Division ( / ) —The process of determining precisely how many times one quantity is contained in, or may be divided by, another quantity: also ÷.
        5. Incrementation ( ++ ) —The process of increasing a quantity by one.
        6. Decrementation ( -- )—The process of decareasing a quantity by one.

      2. Comparison Operators:
        1. Less Than ( < ) —Compares two quantities (numbers) based on whether one is smaller than the other or not.
        2. Greater Than ( > )—Compares two quantities (numbers) based on whether one is larger than the other or not.
        3. Less Than or Equal To ( <= ) —Compares two quantities (numbers) based on whether one is smaller than the other or whether they are the same.
        4. Greater Than or Equal to ( >= ) —Compares two quantities (numbers) based on whether one is larger than the other or whether they are the same.
        5. Not Less Than ( !< ) —Compares two quantities (numbers) based on whether one is NOT smaller than the other.
        6. Not Greater Than ( !> ) —Compares two quantities (numbers) based on whether one is NOT larger than the other.
        7. Equal To ( == ) —Compares two quantities (numbers) based on whether they are the same or not.
        8. Not Equal To ( != ) —Compares two quantities (numbers) based on whether they are NOT the same.

      3. Logical Operators:
        1. And ( && ) —An element used to combine two operations or solutions as one compound operation or one compound solution.
        2. Or ( || ) —An element used to present an option between two or more solutions or operations.
        3. Not ( ! ) —An element which eliminates the possibility of a particular solution or operation.

      4. Assignment Operators:
        1. Assignment ( = ) —An element used to provide a new value to another element such as a variable or array.
        2. Assignment-Addition ( += ) —An operator used to increase the current value of an element by a given number and assign the element the new value.
        3. Assignment-Subtraction ( -= ) —An operator used to decrease the current value of an element by a given number and assign the element the new value.


    2. 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.
      1. 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

         
      2. 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

         
      3. 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".
       
    3. 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:
      1. the function keyword
      2. a unique name that is not one of the reserved JavaScript keywords;
      3. a pair of parentheses to enclose what we will later learn are called parameters, or arguments;
      4. a pair of curly brackets that delimits the extents of the code to be included in the function.
       

      <script language="javascript" type="text/javascript">
       <!--

      1.   function compOp()
      2.   {
      3.    var userNum = prompt("Please type
      4.        a number.","");
      5.    var temp = parseInt(userNum);
      6.    var ans = (temp % 2)? "ODD" : "EVEN";
      7.    alert("Your number, " + temp +
      8.        ", is "+ans);
      9.   }

       //-- >
      </script>

      Now, let us go over this script line-by-line so that we can determine what is happening.

        Line 1:
        function compOp()
        Here is the simple function declaration including:
        1. the function keyword;
        2. the unique name of the function: compOp;
        3. the opening and closing parentheses; and
        4. the opening, line 2, and closing line 9, curly brackets for the entire function.

        Lines 3-4:
        var userNum = prompt("Please type a number.","");
        The first variable declaration and its initialization including:
        1. the var keyword;
        2. the unique name of the variable: userNum;
        3. the assignment operator ( = ); and
        4. the prompt() function which requests data from the user.

        Line 5:
        var temp = parseInt(userNum);
        The second variable declaration and its initialization including:
        1. the var keyword;
        2. the unique name of the variable: temp;
        3. the assignment operator; and
        4. 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.

        Line 6:
        var ans = (temp % 2)? "ODD" : "EVEN";
        The third variable declaration and its initialization including:
        1. the var keyword;
        2. the unique name of the variable: ans;
        3. the assignment operator; and
        4. 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.

        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.
        1. The first term of the concatenated statement is the string—"Your number, ".
        2. 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.
        3. The third term of the concatenation is a very short string, ", is ".
        4. 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".

       
    4. 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.
      1. 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.
      2. 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);



2 Comments:

Anonymous Anonymous said...

carter Is this about you!?? ☆★☆

7:02 AM  
Blogger carter said...

shhhh! it's supposed to be top secret...

1:37 PM  

Post a Comment

<< Home