Loops

Document Sample
Loops Powered By Docstoc
					Strings
Robin Burke
     IT 130
Outline
   Objects
   Strings
       methods
       properties
   Basic methods
   Form validation
Objects
   So far our expressions
       values
       operations
       functions
   Examples
       "a" + "b"
       10 / 3
       alert ("help!")
Objects cont'd
   What about
       document.write (...)?
       arr.length?
       Math.sqrt()
   What does this dot notation mean?
       signals the use of objects
Objects cont'd
   Object
       a bundle combining data and computation
   Example
       traffic light
       could be represented
        by a state variable

                                          state: "green"
Object operations
   We could write a function
    function changeLight (light)
    {
       if (light == "green")
         return "yellow";
         else if (light == "red")
           return "green";
         else
           return "red";
    }

   We could use it as follows
    var trafficLight = "green";
    trafficLight = changeLight (trafficLight);
OK but
   What happens when we elaborate our model
       flashing red
       flashing yellow
       left turn signal
   What if somebody violates our design
       trafficLight = "blue";
   What happens if we want to export our program to
    Mexico
       trafficLight = "rojo";
   We want to link traffic lights in intersections
Objects
   Help
       make code easier to maintain
       make code easier to adapt
       make programs of increasing size and complexity
        possible
   The idea
       combine data and operations together
Traffic Light
   Property
       state
       linked
   Methods
       change ()
       flash (color)
Object syntax
   Objects are created with new
       assign to a variable
    var tl = new TrafficLight ("green");

   Properties and methods
       use variable name dot property
       or variable name dot method call
    tl.state
    tl.change()
    tl.flash("yellow")
Using objects
   We can write
    var northbound = new TrafficLight ("green");
    var southbound = new TrafficLight ("green");
    var eastbound = new TrafficLight ("red");
    northbound.change();
    southbound.change();
    delay ();
    northbound.change();
    southbound.change();
    eastbound.change();
Benefits
   Data is encapsulated in the object
       more elaborate notion of state?
           turn signal
       just add more state variables
           turnSignalState
   Code that modifies the state data
       is associated directly with it
   Whole package is created at once
       easy to make many instances
Object-oriented programming
   Most significant advance in programming
    since high-level languages
       made in the 1980s
   All major languages are now object-oriented
       C++
       C#
       Java
       JavaScript
       Visual Basic
OOP cont'd
   Object-oriented programming
       is largely about defining new objects
       that interact computationally in an application
       linked to each other
   Example
       A PowerPoint presentation object
           contains slide objects
           each slide object
               contains text objects
               contains graphic objects
OOP cont'd
   Beyond the scope of this class
       CSC 211/212 etc.
   We need to know
       how to use the objects JavaScript provides
Arrays
   Arrays (in JavaScript) are objects, too
   Constructed with new
   Property
       length
   Methods
       special syntax with [ ]
Strings
   Objects
   Special constructor
       not new
       quotes
         var str = "foo";

       legal but unnecessary
         var str = new String ("foo");

   Properties
       length
         like array
Some methods
   toUpperCase( )
       converts string to upper case
   toLowerCase( )
       converts string to lower case
   charAt(pos)
       returns character at given position
   substring(start, end)
       returns a string out of the middle
Example
   string.html
Note
   Different calling syntax for Capitalize
       not part of the string object
       string is already part of the language
           can't add new methods
Architecture

                 URL +
                                      User data
                user data




                Request0              Request1

      Client                 Web
                                                     Application
    (Browser)               Server
                Response0            Response1




                                         Result
                                          or
                                     Response page
Form validation
   Problem
     detecting erroneous input
     round-trip to server too slow
     HTML controls limited
   Solution
     use JavaScript to detect bad input
     get user to correct before hitting the server
   Note
     an efficiency tool not a data quality guarantee
     server must validate the data again
     too easy to generate a rogue request
Technique
   Use onSubmit event
       always a button to submit form data to server
   Special syntax
       onSubmit="return fn()"
   if fn returns true
       form data sent to server
   if fn returns false
       form data not sent
Example
   Change of password
   Action
       check that password and retype are the same
   Event
       form submission
   Content needed
       contents of two password fields
Example
   password
   Problems?
       privacy
       prompt
       minimum length
       clear
       focus
Other validation criteria
   valid email address
   integer
   ssn
What to do
   search a string for certain contents
       @ must be in an email address
       no letters in an integer
   search method for string objects
String Search
   Uses special pattern syntax
    /bar/
       the slash is like a quote
       means return the first location of a substring beginning with
        these three characters
   Also
       /[bar]/
       different meaning
       first location of any one of these characters
   Not found
       return -1
       not a legal string index
Validation with search
String Processing
   Many applications require editing of strings
       removing spaces and punctuation
       removing special characters
   Combine search with loops
Basic Idea
   while (pattern is still in string)
       copy useful bits
       search for pattern again
Example
   remove dashes from credit card #
       four ways to accomplish
           character by character
           repeated search whole string
           repeated shorter string
           recursion!
Monday
   Quiz
   Advanced HTML/JS stuff
       positioning

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:6
posted:9/2/2011
language:English
pages:33