Sunday, March 17, 2013

PACS JavaScript March 16th, 2013 Handling Strings



    Thanks to everyone who attened this month's PACS JavaScript SIG.  The following is a summary of my notes and examples I used during the meeting.

    Objectives of Today’s SIG:
    • What is the String Object?
    • The String Object vs the String Literal
    • The String Object's Properties
    • The String Object’s Methods
    • Regular Expressions

    What is a String Object:
    • The string object provides properties and methods to get information about strings or to modify strings. 
    • A String object is created in either of two ways: a programmer creates one by using the new keyword with the constructor function
    • Or JavaScript creates one temporarily when one of the methods is called from a string literal

    The String Object vs the String Literal:
    • Boils down to how they are created.
    • The String Object:
      • Syntax : var name = new String (“string value”);
      • Example: var presenter = new String (“Andrew”);
    • The String Literal
      • Syntax : var name = “value”;
      • Example: var presenter = “Andrew”;

    Example script
    <script type="text/javascript">
    var presenter1="Andrew";
    var presenter2="Andrew";
    // var presenter1 = new String ("Andrew");
    // var presenter2 = new String ("Andrew");
    if (presenter1 == presenter2){
    window.alert("The strings are the same");
    }
    else {
    window.alert("The strings are not the same!");
    }
    // Because a string object is and object value and not a literal value
    // Reminder: try it with one object and one literal
    </script>

    String Object Properties:
    • String Objects have only three properties
      • Length
        • Returns the length of the string (number of Characters)
      • Prototype
        • Used to add properties or methods to a string
      • Constructor
        • Returns the value of the constructor function.

    Here are the property examples that I used in the SIG

    <script type="text/javascript">
    // length property
    var presenter="Andrew";
    document.write("The name has "+presenter.length+" characters.</br>");

    // Prototype property
    String.prototype.status="wide awake";
    document.write(presenter+" is "+presenter.status+"</br>");

    The constructor Property Sends back the value of the constructor function

    // constructor property
    function sigleader(name,condition){
    this.name=name;
    this.condition=condition;
    }
    var guy=new sigleader("Don Arrowsmith","exhausted");
    document.write(guy.name+" is "+guy.condition+"</br>");
    document.write(guy.constructor);

    /* The constructor property makes absolutely no practical difference to anything internally.
    It's only any use if your code explicitly uses it. For example, you may decide you need each
    of your objects to have a reference to the actual constructor function that created it; if so,
    you'll need to set the constructor property explicitly when you set up inheritance by assigning
    an object to a constructor function's prototype property.
    */

    String Object Methods:
    • Methods are instructions to our Object
    • Two basic types of String Object Methods
      1. Methods that add HTML Tags
      1. Methods that obtain information about or alter a string

    I've supplied the following method examples from the SIG.

    The fontcolor() Method adds font color to the text string that is used to call it.
    It takes in a string parameter that indicates what color the text should be
    syntax = <font color="color_value">text_string </font>

    // The fontcolor() method
    var the_text1 = "I am so mad I am red";
    document.write(the_text1.fontcolor("red")+"</br>");

    The fontsize() Method adjusts the font size of the text string that calls the method.
    It takes in a numeric value to represent the size of the text (between 1 and 7).

    syntax = <font size="number">text_string</font>

    // the fonsize() method
    var the_text2 = "I am so huge";
    document.write(the_text2.fontsize(7)+"</br>");
    var the_text2 = "I am so small";
    document.write(the_text2.fontsize(2)+"</br>");

    // combine the two
    var the_text3 = "I'm hugely mad";
    document.write(the_text3.fontcolor("red").fontsize(10)+"</br>");

    The link() Method works like the anchor() method, but instead it creates a typical hyperlink on the page.
    It takes in a string parameter that is the value of the URL.
    syntax = <a href="url"> text_string </a>

    // attaching a link using the link() method
    var the_text4 = "When I'm hugely mad I like to go here"
    var full_link = the_text4.link("http://www.gohawaii.com/");
    document.write(full_link);

    The 4th set of examples show how certain String Methods can be used to gather information about the string similar to how an object's properties can. 

    The charAt() Method determines which character resides at a particular position in a string.  As with arrays the position count begins with 0 (zero).  IF you want to find the last character, you need to know how many characters are in the string before you use the method. You can use the length property to determine the number of characters in the string

    // finding a character using charAt() Method
    var the_text1="Welcome to PACS JavaScript SIG";
    var which_char=the_text1.charAt(0);
    document.write("The first character is "+which_char+"</br>");
    document.write("The first character is "+the_text1.charAt(12)+"</br>");

    // using the length property to find the last character
    var position = the_text1.length-1;
    var last_char = the_text1.charAt(position);
    document.write("The last character is "+last_char+"</br>");

    The charCodeAt() Method works the same way as the charAt() method, but returns the character code of the character at the position sent as the parameter.
    HTML character code

    // return the character code with the charCodeAt() Method
    var which_char1=the_text1.charCodeAt(0);
    document.write("The first character is "+which_char1+"</br>");
    document.write("The first character is "+the_text1.charCodeAt(12)+"</br>");

    The concat() Method method works much like the Array Object's concat() method.  It combines the text string that calls the method with any number of other text strings sent as parameters and returns the combined value.

    // combine multiple strings together using the concat() Method
    var the_text0 = "Welcome to the ";
    document.write(the_text0.concat(the_text1)+"</br>");

    The fromCharCode() method creates a string from a series of character codes sent as parameters to the method.

    // The fromCharCode() Method
    document.write("This code equals "+String.fromCharCode(72,73)+"</br>");

    The indexOf() Method finds out where a certain character or string begins in a string.  It then returns the position of only the first occurrence of the character or string that is set as the parameter.  A "-1" value is returned if it isn't found.  The position count begins at 0 and the method is case sensitive.  Adding an integer at the second parameter, dictates the starting point where the count begins.

    // The indexOf() Methodl (First instance)
    var position = the_text1.indexOf("A");
    document.write(the_text1+"</br>");
    document.write(position+"</br>");
    // The lastIndexOf() Method (Last instance)
    var position2=the_text1.lastIndexOf("A");
    document.write(position2+"</br>");

    Try changing out the "A" with a "a" and see what values you get.

    My fifth set of examples are a sample of string methods that can manipulate the text.  The following are the methods, a description of how they work and a sample script.

    The slice() Method pulls out a portion of a string and returns a new string.  The text string that was sliced
    takes in two numeric parameters to tell it where to begin and end the portion of the string to be pulled. 

    var the_text1="Welcome to PACS JavaScript SIG";
    // The slice() Method
    var the_text2=the_text1.slice(11,15);
    document.write(the_text1+"</br>");
    document.write(the_text2+"</br>");

    The substr() Method pulls out a portion of a string and returns the portion that is removed as a new string.  It takes two numeric parameters, the first parameter specifies the beginning of the removal and the second parameter specifies how many characters to remove.

    // The substr() Method
    var the_text3=the_text1.substr(11,15);
    document.write(the_text1+"</br>");
    document.write(the_text3+"</br>");

    The substring() Method works much like the substr() method, but it allows you to send parameters for the beginning position and the ending position of the portion for the string you want to remove.  It then returns the removed portion as a new string.

    // The substring() Method
    var the_text4=the_text1.substring(11,15);
    document.write(the_text1+"</br>");
    document.write(the_text4+"</br>");

    The toLowerCase() Method returns in lowercase letters the value of the string that called it.

    // The toLowerCase() Method
    document.write(the_text1.toLowerCase()+"</br>");

    The toUpperCase() Method returns in uppercase letters the value of the string that called it.

    // The toUpperCase() Method
    document.write(the_text1.toUpperCase()+"</br>");

    Next Month we'll start off by talking about Regular Expressions:

Sunday, February 17, 2013

PACS JavaScript February 16th, 2013 Math, Number, and Date Objects




    Objectives of Today’s SIG:
    • Picking up where we left off
      • Associative Arrays
    • Learning about
      • Math Object
      • Number Object
      • Date Object

    Arrays Con’t:
    • Using Associative Arrays
      • Also referred to as hash tables
      • Creating Custom properties

    Using Associative Arrays
    • Assign the tag MVP to one of the players on your roster.


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>PACS JavaScript SIG</title>
    </head>
    <body>
    <h1>PACS JavaScript SIG/</h1>

    <script type="text/javascript">

    // Usint Associative Arrays
    var positions = new Array();
    positions["3B"] = "Kevin Fandsen";
    positions["2B"] = "Chase Utley";
    positions["1B"] = "Ryan Howard";
    positions["SS"] = "Jimmy Rollins";
    positions["P"] = "Roy Halladay";
    positions["C"] = "Sebastian Valle";
    document.write("Playing First Base is "+positions["1B"]+"</br>");

    </script>

    </body>
    </html>


    Math Object:
    Like all Objects, the Math Object has properties and Methods. 

    • Math Properties
    Here are some examples of the Math Properites used during the SIG

    <script type="text/javascript">
    var bob = 1
    var math1 = bob * Math.PI;
    document.write("The value of PI "+math1+"</br>");

    //Use to calculate area of a circle
    var math2 = bob * Math.E;
    document.write("Euler's Constant "+math2+"</br>");

    /*  It is the limit of (1 + 1/n)^n as n approaches infinity, an expression that arises
    in the study of compound interest, and can also be calculated as the sum of the
    infinite series[2] */

    var math3 = bob * Math.SQRT2;
    document.write("The value of PI "+math3+"</br>");

    //square root of 2 is used in a 45degree triangle/pathagram theroy

    </script>

    • Math Methods
    We broke the Math Methods into three different groups
    • The Basic Methods
    • The Two-Parameter Methods
    • The Other Methods

    <script type="text/javascript">
    var bob = 4.5;
    var mathabs = Math.abs(bob);
    document.write("Here is "+mathabs+"</br>");
    var mathexp = Math.exp(bob);
    document.write("Here is "+mathexp+"</br>");
    var mathsqrt = Math.sqrt(bob);
    document.write("Here is "+mathsqrt+"</br>");
    var mathmx = Math.max(mathabs,mathexp);
    document.write("Here is "+mathmx+"</br>");
    var mathrnd = Math.round(mathexp);
    document.write("Here is "+mathrnd+"</br>");
    document.write("Here is "+bob+"</br>");
    </script>


    Number Object:
    • Number Object
      • Number Object Properties
      • Number Object Methods

    Number Object Properties:
    • MAX_VALUE
    • MIN_VALUE
    • NaN
    • NEGATIVE_INFINITY
    • POSITIVE_INVINITY

    <script type="text/javascript">
    // var num1 = 7.5
    var num1 = new Number(7.5);
    var num2 = new Number("127");
    var num3 = new Number("Bob");
    // sets up variable for display
    var num3a = Number.MAX_VALUE;
    var num3b = Number.MIN_VALUE;
    var num3c = Number.NEGATIVE_INFINITY;
    var num3d = Number.POSITIVE_INFINITY;
    document.write("The value of our variable is "+num1+"</br>");
    document.write("The value of our variable is "+num2+"</br>");
    var num4=num1+num2
    document.write(num4+"</br>");
    document.write("The value of our variable is "+num3+"</br>");
    document.write("The value of our variable is "+num3a+"</br>");
    document.write("The value of our variable is "+num3b+"</br>");
    document.write("The value of our variable is "+num3c+"</br>");
    document.write("The value of our variable is "+num3d+"</br>");

    </script>


    Number Object Methods:
    • toExponential()
    • toFixed()
    • toPrecision()
    • toSource()
    • toString()
    • valueOf()

    <script type="text/javascript">
    var num1=145.27456789
    document.write(num1.toExponential()+"</br>");
    document.write(num1.toFixed(3)+"</br>");
    //rounded to the specific number of digits after the decimal
    document.write(num1.toPrecision(3)+"</br>");
    //total number of characters not past decimal point
    document.write(num1.toString()+"</br>");

    </script>

    Date Object:
    • Date Object Properties
      • Constructor
      • prototype
    • Date Object Methods
      • A lot of them
      • Consult pg. 328 – 329 for list
      • Organize the Methods by their functions
        • Get Values
        • Set Values
        • Other
    <script type="text/javascript">
    var rightnow=new Date();
    var theday=rightnow.getMinutes();
    document.write("Today's date is "+theday+"</br>");
    </script>

Sunday, January 20, 2013

PACS JavaScript, January 19th, 2013 JavaScript Arrays


    Objectives of Today’s SIG:
    • Learn what is an Array, Why do we use them, and how do we create them.
    • Different Properties and Methods of the Array Object
    • Using Arrays with Loops
    • Using Associative Arrays

    Creating An Array:
    What is an Array?
    Array =  a way of storing data of similar types for easy access in a script.  A user defined object that is accessed in a different way than other objects are accessed.
    Why Arrays are Useful?
    To make it easier to access the stored information.
    Defining and Accessing Arrays
    var arrayname = new Array()

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>PACS JavaScript SIG</title>
    </head>
    <body>
    <h1>PACS JavaScript SIG/</h1>

    <script type="text/javascript">
    //Creating the Array
    var pitcher_names = new Array("Roy Halladay","Cole Hamels","Cliff Lee","Jonathan Pettibone");
    var ws_mvp = pitcher_names[1];
    // Remember 1=2
    document.write ("The MVP for the 2008 World Series was "+ws_mvp+"</br>");
    // create array and then add to it
    var infield1_name = new Array();
    infield1_name[0] = "Kevin Frandsen";
    infield1_name[1] = "Freddy Galvis";
    infield1_name[3] = "Ryan Howard";
    infield1_name[5] = "Cesar Hernandez";
    infield1_name[6] = "Jimmy Rollins";
    infield1_name[4] = "Chase Utley";
    infield1_name[2] = "Michael Young";
    infield1_name[29] = "Andrew Schooler";
    var clean_up = infield1_name[30];
    document.write("Now batting clean up "+clean_up+"</br>");



    </script>

    </body>
    </html>


    Array Objects:
    Understanding the Properties and Methods of the Array Object.
    Properties
    The Length Property
    The prototype Property

    <script type="text/javascript">
    var pitcher_names = new Array("Roy Halladay","Cole Hamels","Cliff Lee","Jonathan Pettibone");
    var infield1_name = new Array();
    infield1_name[0] = "Kevin Frandsen";
    infield1_name[1] = "Freddy Galvis";
    infield1_name[3] = "Ryan Howard";
    infield1_name[5] = "Cesar Hernandez";
    infield1_name[6] = "Jimmy Rollins";
    infield1_name[4] = "Chase Utley";
    infield1_name[2] = "Michael Young";
    // Length Property
    document.write("The infield1_name array has "+infield1_name.length+" elements.</br>");
    // Prototype Property
    Array.prototype.standing = "Bad team";
    Array.prototype.standing2 = "Good team";
    document.write("Currently the team's infield ranks "+infield1_name.standing+"</br>");
    document.write("Currently the team's pitching ranks "+pitcher_names.standing2+infield1_name.standing+"</br>");
    // document.write("Currently the team's pitching ranks "+infield1_name[6].standing2+"</br>");

    </script>

    Methods
    concat
    join
    sort
    toString
    slice
    splice
    pop, push, shift and unshift

    <script type="text/javascript">
    var pitcher_names = new Array("Roy Halladay","Cole Hamels","Cliff Lee","Jonathan Pettibone");
    var infield1_name = new Array();
    infield1_name[0] = "Kevin Frandsen";
    infield1_name[1] = "Freddy Galvis";
    infield1_name[3] = "Ryan Howard";
    infield1_name[5] = "cesar Hernandez";
    infield1_name[6] = "Jimmy Rollins";
    infield1_name[4] = "Chase Utley";
    infield1_name[2] = "Michael Young";
    // Methods
    // join method
    var pitchers1 = pitcher_names.join();
    document.write("The Phillies Pitching Staff: "+pitchers1+"</br>");
    var pitchers2 = pitcher_names.join(", ");
    document.write("The Phillies Pitching Staff: "+pitchers2+"</br>");
    var pitchers3 = pitcher_names.join(" : ");
    document.write("The Phillies Pitching Staff: "+pitchers3+"</br>");

    // concat method
    var catchers_name = new Array ("Erik Kratz","Sebastian Valle");
    var catchers1 = catchers_name.join(", ");
    document.write("The Phillies Catchers: "+catchers1+"</br>");
    var pitchers2 = pitcher_names.join(", ");
    document.write("The Phillies Pitching Staff: "+pitchers2+"</br>");
    // adding the .join at the end
    var pitch_n_catch = pitcher_names.concat(catchers_name).join("");
    document.write("00 The following will report to Camp on Feb. 21st: "+pitch_n_catch+"</br>");
    var line_up = pitcher_names.concat(catchers_name,infield1_name);
    document.write("The following will report to Camp on March 1st: "+line_up+"</br>");

    // sort method
    var infield_alpha = infield1_name.sort();
    document.write("01 Names in alphabetical order "+infield_alpha+"</br>");
    var infield_alpha = infield1_name;
    document.write("01 Names in alphabetical order "+infield_alpha+"</br>");

    // reverse method
    var infield_rev = infield1_name.reverse();
    document.write("02 Names in reverse order "+infield_rev+"</br>");

    // toString method
    var pitchers3 = pitcher_names.toString();
    document.write(pitchers3+"</br>");

    // slice method
    var free_agents = infield1_name.slice(0,2);
    document.write("Look who's up for free agencey: "+free_agents+"</br>");

    // splice method
    document.write("splice method</br>");
    var outfield_names = new Array("Domonic Brown", "Zach Collier", "Tyson Gillies", "Ender Inciarte", "John Mayberry", "Laynce Nix", "Ben Revere", "Darin Ruf");
    document.write("In the Outfield: "+outfield_names+"</br>");
    var outfield_out = outfield_names.splice(2,2);
    document.write("Out of the outfield: "+outfield_out+"</br>");
    document.write("In the Outfield: "+outfield_names+"</br>");
    outfield_names.splice(5,2,outfield_out);
    document.write("In the Outfield: "+outfield_names+"</br>");

    // pop, push, shift, and unshift methods
    document.write("In the Outfield1: "+outfield_names+"</br>");
    var outfield1 = outfield_names.pop();
    document.write("Outfield popped: "+outfield1+"</br>");
    document.write("In the Outfield now: "+outfield_names+"</br>");
    outfield_names.push(outfield1);
    document.write("In the Outfield now: "+outfield_names+"</br>");
    outfield2=outfield_names.shift();
    document.write("Outfield shifted: "+outfield2+"</br>");
    document.write("In the Outfield now: "+outfield_names+"</br>");
    outfield_names.unshift(outfield2);
    document.write("In the Outfield now: "+outfield_names+"</br>");



    </script>


    Array Objects:
    Using Arrays with Loops
    Using Associative Arrays


    //Using Arrays with Loops
    //Creating Array Elements
    var s_list = new Array(4);
    for(var count=0; count<4; count++){
    s_list[count]=window.prompt ("Enter a name","");
    }
    var s_list1=s_list.toString();
    document.write(s_list1+"</br>");


    Next Month:
    Math, Number, and Date