Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings Robin Burke IT 130. Outline Objects Strings methods properties Basic methods Form validation.

Similar presentations


Presentation on theme: "Strings Robin Burke IT 130. Outline Objects Strings methods properties Basic methods Form validation."— Presentation transcript:

1 Strings Robin Burke IT 130

2 Outline Objects Strings methods properties Basic methods Form validation

3 Objects So far our expressions values operations functions Examples "a" + "b" 10 / 3 alert ("help!")

4 Objects cont'd What about document.write (...)? arr.length? Math.sqrt() What does this dot notation mean? signals the use of objects

5 Objects cont'd Object a bundle combining data and computation Example traffic light could be represented by a state variable state: "green"

6 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);

7 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

8 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

9 Traffic Light Property state linked Methods change () flash (color)

10 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")

11 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();

12 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

13 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

14 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

15 OOP cont'd Beyond the scope of this class CSC 211/212 etc. We need to know how to use the objects JavaScript provides

16 Arrays Arrays (in JavaScript) are objects, too Constructed with new Property length Methods special syntax with [ ]

17 Strings Objects Special constructor not new quotes var str = "foo"; legal but unnecessary var str = new String ("foo"); Properties length like array

18 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

19 Example string.html

20 Note Different calling syntax for Capitalize not part of the string object string is already part of the language can't add new methods

21 Architecture

22 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

23 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

24 Example Change of password Action check that password and retype are the same Event form submission Content needed contents of two password fields

25 Example password Problems? privacy prompt minimum length clear focus

26 Other validation criteria valid email address integer ssn

27 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

28 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

29 Validation with search

30 String Processing Many applications require editing of strings removing spaces and punctuation removing special characters Combine search with loops

31 Basic Idea while (pattern is still in string) copy useful bits search for pattern again

32 Example remove dashes from credit card # four ways to accomplish character by character repeated search whole string repeated shorter string recursion!

33 Monday Quiz Advanced HTML/JS stuff positioning


Download ppt "Strings Robin Burke IT 130. Outline Objects Strings methods properties Basic methods Form validation."

Similar presentations


Ads by Google