Sunday, January 28, 2007

Spring 2007

week 2: 1/26


  1. TOPICS:
    • Data Types:
      • LINK   Numerical Data,
      • LINK   String Data,
      • LINK   Boolean Data,
    • Basic Built-in Functions:
      • LINK   alert() function,
      • LINK   document.write() function,
      • LINK   confirm() function,
      • LINK   prompt() function,
      • LINK   parseInt() function,
    • Miscellaneous:
      • LINK   Concatenation vs. Addition,
      • LINK   Variables,


  2. HOMEWORK:
    1. From class—Using Notepad, Textpad, or TextEdit with a Mac, please type an .html document with a script that does the following:
      1. First, declares a variable with the name num1.
      2. Second, initializes that variable with a prompt() function.
      3. The prompt() function should request a number from the user.
      4. Third, declares a second variable with the name num2.
      5. Fourth, initializes this second variable with another prompt() function.
      6. The prompt() function should request a second number from the user.
      7. Fifth, declares a third variable with a numerical operation that adds together num1 and num2.
      8. Sixth, uses an alert() function to display both the numerical operation and its sum.
    2. From the book—Do question 5 at the end of chapter 3:

  3. REVIEW: Last week, you were introduced to the three types of data defined by JavaScript:
    1. JavaScript Data Types  
      1. Numerical Data—This is perhaps the easiest data type to recognize as it pertains to something that we can intuitively recognize, things that we deal with on a daily basis, numbers. It includes all numbers, both positive (greater than zero) and negative (less than zero), integers (whole numbers such as 1, 2, 3, -17, 200, etc.), and decimals (1.5, .23, 22.87, etc). Moreover, it also includes numerical expressions; that is, numbers expressed in terms of equations, simple equations such as 1 + 1, more complex equations such as 5 * ((5 + 1) / (25 - 8)), as well as much more complicated mathematical equations that include geometrical, algebraic, and trigonometric functions, and calculus.  
      2. String Data—This type is also relatively intuitive as the term itself, STRING, refers simply to a 'string of characters' set between quotation marks. The term characters here refers to any alpha-numeric character (numbers and letters), both lower and upper case, the hypen, the underscore, as well as all the other characters used in HTML. String data can be common English, such a a word ("hello"), a phrase ("very good food"), or longer strings such as sentences or paragraphs, or even whole 'pages' of HTML. Or, it may be some non-sensical series of character lined-up one after the other. 
      3. Boolean Data—This type simply includes the values TRUE and FALSE, and their numerical equivalents, 1 and 0.

    2. JavaScript Basic Built-in Functions
        You have also been introduced to some basic built-in JavaScript functions. Remember, a function is a an action that JavaScript takes. The built-in functions we have covered up to now are as follows: 
      1. alert()—This is perhaps the easiest function to remember as it causes the browser to display a small 'alert' window. It is designed as a ONE-WAY communication of information (data) from the browser or script to the user. It is NOT interactive in that the user cannot respond by sending any information of his/her own. 
      2. document.write()—Like the alert(), the document.write() is designed as a ONE-WAY communication of information (data) from the browser or script to the user. It is NOT interactive in that the user cannot respond by sending any information of his/her own; however, it does not produce an alert box. Instead, the information is 'written' directly into the browser window, the page itself, otherwise known as the 'document'. For this reason, the information sent from the script or browser, may also contain HTML or CSS.  
      3. prompt()—This function is similar to the alert box in that it produces a small window that pops up above the browser window. Furthermore, like the alert(), some information or data is sent by the script to the user, usually in the form of a request for information. However, as a result of this 'request', this function IS in fact interactive which makes it a TWO-WAY exchange of information. This is because the user must provide some data of his/her own back to the script. The user thusly affects the outcome of the script. The type of data the user sends back is restricted only by the request made. 
      4. confirm()—This function is also similar to the alert function in that it produces a small window that pops up above the browser window. Furthermore, it is similar to the prompt() function as well in that this function is also interactive. The user here must also provide some data of his/her own back to the script, and likewise affecting the outcome. However, the type of data the user sends back may only be in the form of boolean data as the space provided for responding to the request in the confirm box comes only in the form of 2 buttons, OKAY (true) and CANCEL (false). The request, then, must fit the restraints of this model in that TRUE or FALSE must be satisfactory responses.
        EXAMPLES:

            Good—Click okay if you need more time.
            Bad—What color is your hair? 
      5. parseInt()—This function is different than the other four listed above in that it does not necessarily produce any physical changes or results, such as the appearance of a pop-up window, or the change of the appearance of the web-page itself. What it does is perhaps more typical of most JavaScript functions as it works internally on the script itself, more specifically, altering the data within the script somehow. The task of this function is to change one type of data into another type of data, from STRING DATA to NUMERICAL DATA. The term parseInt means to parse the data provided as an integer; and broken down into plain English, means to PROCESS THE INFORMATION AS A NUMBER. The string data is placed between the parentheses, and the function, parseInt(), will therefore convert it from a string to a number, such as:
            parseInt("1001") = 1001 >> converting the string "1001" to the integer 1001.

       
    3. Variables—A variable in JavaScript is a special item. It serves as a container, like a cup, but which instead of containing liquids and drinks stores a single piece of information, or data. Once created, a variable may contain any piece of data of any type (numerical, string, boolean), but it may only contain one element of data at a time. This piece of data that it stores is known as its value. Moreover, this value may be changed at any time and as many times as required.
          A variable is created, or declared with two things:
      1. var keyword
      2. unique name

      A variable is then given some data to hold, assigned a value, by using the assignment operator   = and then its value. To give a variable some data, to assign it a value, for the first time is called initializing a variable.
      1. declaring a variable —> var myName
      2. initializing a variable —> myName = "Carter"

       
    4. Concatenation—Technically, this is just one of many different JAVASCRIPT OPERATIONS; however, because of its importance and widespread, ubiquitous usage, I have discussed it since the first class. Furthermore, I believe it deserves review and mention here. Its actual definitions are as follows:
      1. To connect or link in a series or chain.
      2. (Computer Science) To arrange (strings of characters) into a chained list.
      Both above are significant to us: it is simply placing one CHARACTER after another in a chain, or even one chain of characters after another chain of characters. What we are speaking about here then is STRING data.In order for concatenation to occur, there must be string data present, but it may also involve other types of data, as the example below suggests. The two (2) short strings here combined:
          "Hello, " + "how are you?"
      result in the longer string
          —>
      "Hello, how are you"
      Here are some other examples of concatenation:
          "Carter" + "Johnson",
                  "CarterJohnson"

          "March" + 11, "March11"

          "March" + "11", "March11"

          "March + 11", "March + 11"

          "March + 11 =" + "March" + "11",
                  "March + 11 = March11"

     
  4. INTRODUCE: Friday, we began working on a short script that put to use much of what we have covered in the first two classes. Most importantly, it requires that you begin to understand the jargon that is used to describe the computer processes encapsulated in a script. We'll go through it step by step as we did in class:
    1. Step 1: declare a variable with the name num1—to declare a variable simply means to create a new variable, and we do this with two (2) terms: the var keyword and a unique name. For example:
        var num1;


    2. Step 2: initialize that variable with a prompt() function
    3. Step 3: The prompt() function should request a number from the user—to initialize a variable means to give it a value for the first time, and we do this by using the assignment operator ( = ), followed by an element of data, an operation, or a function that produces an element of data. For example, in what follows, we initialize num1 with a prompt() function which results in an element of data:
        var num1 =
          prompt("Please type a number below","")
      ;


    4. Step 4: declare a second variable with the name num2—this step follows along a similar pattern to step 1:
        var num1 =
          prompt("Please type a number below","");
        var num2;


    5. Step 5: initialize this second variable with another prompt() function.
    6. Step 6: The prompt() function should request a second number from the user—this step follows along a similar pattern to step 2:
        var num1 =
          prompt("Please type a number below","");
        var num2 =
          prompt("Please type a SECOND number below","")
      ;


    7. Step 7: declare a third variable named userSum.
        var num1 =
          prompt("Please type a number below","");
        var num2 =
          prompt("Please type a SECOND number below","");
        var userSum;


    8. Step 8: initialize the third variable with a numerical operation that adds together num1 and num2—instead of providing a prompt() function to provide a value for this variable, in this step, we add the previous two variables together. This is known as a numerical operation.
        var num1 =
          prompt("Please type a number below","");
        var num2 =
          prompt("Please type a SECOND number below","");
        var userSum = num1 + num2;


    9. Step 9: alert the user to the value of userSum—for this step, we simply place the variable, userSum, inside an alert() function to reveal its value.
        var num1 =
          prompt("Please type a number below","");
        var num2 =
          prompt("Please type a SECOND number below","");
        var userSum = num1 + num2;
        alert(userSum);


      What we see as a result of this last step, however, is NOT a sum at all; but rather, the result of concatenation. If the user were to type the number 10 as a result of the first prompt(), and then the number 20 as a result of the second prompt(), the result that would appear in the final alert() function would be 1020.
          Seeing this in the alert() box at first might be a bit perplexing, since we should all know that the sum of 10 and 20 is 30. Why then the 1020 result? Obviously, something else has happened instead of addition. As mentioned, what occurred instead is concatenation. But why would this happen even though the user types numbers at the prompt() ? The answer is that the prompt() function always results in string data, even if what is typed in the box is a number. Therefore, instead of addition:

        10 + 20 = 30;

      we will have concatenation:

        "10" + "20" = "1020";

      That is why we would see something like 1020 in the alert() function. But then the question is: how do we change the string data from the prompt() into numerical data? And the answer to that would be by using the parseInt() function.

    10. Step 10: use the parseInt() function to convert the string data that comes from the prompt() function into numerical data. Let us go slowly and take this step-by-step. Here is what we know so far, that the following statement, for example, results in string data:
        var num1 =
          prompt("Please type a number below","");

      This means that whatever the user types at the prompt() function, even if it is a number, will be a string and it will be stored in the num1 variable. We also know how the parseInt() function works like so:

        var num1 =
          parseInt(a string goes here)

      Above, we can see that an element of string data goes in between the parentheses of the parseInt() function, converting the string to an integer (a whole number) which will be stored in the variable, num1. For example:

        var num1 =
          parseInt("1001")

      As a result of the parseInt() function, the string "1001" is converted into the integer 1001. Once this occurs, the number is placed inside of the num1 variable.
          Now, if we put together what we know about the prompt() function and the parseInt() function, we might come out with a satisfying result:

      1. The prompt() function results in a string that the user types in the blank space provided, even if the user types a number there;
      2. The parseInt() function converts the string data in between its parentheses into numerical data;
      3. A variable stores a single element of data, whether it be numerical, string, or boolean.
      4. The assignment operator ( = ) transfers a value on its right side into a variable, for example, on its left side.
        var num1 =
          parseInt(prompt("Please type a number below",""))

        Above, the string from the prompt() function is converted into an integer by the parseInt() function, transferred by the assignment operator ( = ) into and stored by the variable, num1. As a result of all 10 steps, we get the following little script:

        var num1 =
          parseInt(prompt("Please type a number below",""));

        var num2 =
          parseInt(prompt("Please type a SECOND number below",""));

        var userSum = num1 + num2;

        alert(userSum);




Saturday, January 20, 2007

Spring 2007

week 1: 1/19

Hello everyone. First I want to welcome everybody back to TCI after the winter break from classes. I hope you all enjoyed your holidays, and although I know they're never long enough, I hope you all are glad to be back. For me, it's always nice to be back in class after some time off.

But now you have additional reasons for your happiness: if you have gotten this far, that is, if you have reached this class, DMA106-JavaScript, then you are probably more than half way through your time at TCI. Some of you are perhaps even further along. So, congratulate yourselves, and pat youselves on the back for trudging through the dull humdrum of PhotoShop and Flash, and finally getting to the glitz and glamour of the exciting world of JavaScript!

Have a nice weekend, and I'll see you next Friday, Carter-

  1. TOPICS:
    • LINK   Data Types,
    • LINK   Built-in Functions,
    • LINK   Variables,
    • LINK   Concatenation

  2. HOMEWORK:
    • Reading—Chapters 1 & 2 in the text
    • Exercises—Do all eleven (11) exercises at the end of Chapter 2

     
  3. INTRODUCE:
    1. JavaScript Data Types
      1. Numerical Data—This is perhaps the easiest data type to recognize as it pertains to something that we can intuitively recognize, things that we deal with on a daily basis, numbers. It includes all numbers, both positive (greater than zero) and negative (less than zero), integers (whole numbers such as 1, 2, 3, -17, 200, etc.), and decimals (1.5, .23, 22.87, etc). Moreover, it also includes numerical expressions; that is, numbers expressed in terms of equations, simple equations such as 1 + 1, more complex equations such as 5 * ((5 + 1) / (25 - 8)), as well as much more complicated mathematical equations that include geometrical, algebraic, and trigonometric functions, and calculus.
      2. String Data—This type is also relatively intuitive as the term itself, STRING, refers simply to a 'string of characters' set between quotation marks. The term characters here refers to any alpha-numeric character (numbers and letters), both lower and upper case, the hypen, the underscore, as well as all the other characters used in HTML. String data can be common English, such a a word ("hello"), a phrase ("very good food"), or longer strings such as sentences or paragraphs, or even whole 'pages' of HTML. Or, it may be some non-sensical series of character lined-up one after the other.
      3. Boolean Data—This type simply includes the values TRUE and FALSE, and their numerical equivalents, 1 and 0.

       
    2. JavaScript Basic Built-in Functions
        You have also been introduced to some basic built-in JavaScript functions. Remember, a function is a an action that JavaScript takes. The built-in functions we have covered up to now are as follows:
      1. alert()—This is perhaps the easiest function to remember as it causes the browser to display a small 'alert' window. It is designed as a ONE-WAY communication of information (data) from the browser or script to the user. It is NOT interactive in that the user cannot respond by sending any information of his/her own.
      2. document.write()—Like the alert(), the document.write() is designed as a ONE-WAY communication of information (data) from the browser or script to the user. It is NOT interactive in that the user cannot respond by sending any information of his/her own; however, it does not produce an alert box. Instead, the information is 'written' directly into the browser window, the page itself, otherwise known as the 'document'. For this reason, the information sent from the script or browser, may also contain HTML or CSS.
      3. prompt()—This function is similar to the alert box in that it produces a small window that pops up above the browser window. Furthermore, like the alert(), some information or data is sent by the script to the user, usually in the form of a request for information. However, as a result of this 'request', this function IS in fact interactive which makes it a TWO-WAY exchange of information. This is because the user must provide some data of his/her own back to the script. The user thusly affects the outcome of the script. The type of data the user sends back is restricted only by the request made.
      4. confirm()—This function is also similar to the alert function in that it produces a small window that pops up above the browser window. Furthermore, it is similar to the prompt() function as well in that this function is also interactive. The user here must also provide some data of his/her own back to the script, and likewise affecting the outcome. However, the type of data the user sends back may only be in the form of boolean data as the space provided for responding to the request in the confirm box comes only in the form of 2 buttons, OKAY (true) and CANCEL (false). The request, then, must fit the restraints of this model in that TRUE or FALSE must be satisfactory responses.
        EXAMPLES:

            Good—Click okay if you need more time.
            Bad—What color is your hair?
      5. parseInt()—This function is different than the other four listed above in that it does not necessarily produce any physical changes or results, such as the appearance of a pop-up window, or the change of the appearance of the web-page itself. What it does is perhaps more typical of most JavaScript functions as it works internally on the script itself, more specifically, altering the data within the script somehow. The task of this function is to change one type of data into another type of data, from STRING DATA to NUMERICAL DATA. The term parseInt means to parse the data provided as an integer; and broken down into plain English, means to PROCESS THE INFORMATION AS A NUMBER. The string data is placed between the parentheses, and the function, parseInt(), will therefore convert it from a string to a number, such as:
            parseInt("1001") = 1001 >> converting the string "1001" to the integer 1001.

       
    3. Variables—A variable in JavaScript is a special item. It serves as a container, like a cup, but which instead of containing liquids and drinks stores a single piece of information, or data. Once created, a variable may contain any piece of data of any type (numerical, string, boolean), but it may only contain one element of data at a time. This piece of data that it stores is known as its value. Moreover, this value may be changed at any time and as many times as required.
          A variable is created, or declared with two things:
      1. var keyword
      2. unique name

      A variable is then given some data to hold, assigned a value, by using the assignment operator   = and then its value. To give a variable some data, to assign it a value, for the first time is called initializing a variable.
      1. declaring a variable —> var myName
      2. initializing a variable —> myName = "Carter"

       
    4. Concatenation—Technically, this is just one of many different JAVASCRIPT OPERATIONS; however, because of its importance and widespread, ubiquitous usage, I have discussed it since the first class. Furthermore, I believe it deserves review and mention here. Its actual definitions are as follows:
      1. To connect or link in a series or chain.
      2. (Computer Science) To arrange (strings of characters) into a chained list.
      Both above are significant to us: it is simply placing one CHARACTER after another in a chain, or even one chain of characters after another chain of characters. What we are speaking about here then is STRING data.In order for concatenation to occur, there must be string data present, but it may also involve other types of data, as the example below suggests. The two (2) short strings here combined:
          "Hello, " + "how are you?"
      result in the longer string
          —>
      "Hello, how are you"
      Here are some other examples of concatenation:
          "Carter" + "Johnson",
                  "CarterJohnson"

          "March" + 11, "March11"

          "March" + "11", "March11"

          "March + 11", "March + 11"

          "March + 11 =" + "March" + "11",
                  "March + 11 = March11"