1 / 18

ECA 225

ECA 225. Applied Online Programming. strings. this keyword. passes a value to a function based solely on the context of where this is used can be used to pass entire objects, such as all values submitted through a form. <form name=‘myForm’ onSubmit=‘process_form( this ) >.

declan-hahn
Download Presentation

ECA 225

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ECA 225 AppliedOnline Programming strings ECA 225 Applied Interactive Programming

  2. this keyword • passes a value to a function based solely on the context of where this is used • can be used to pass entire objects, such as all values submitted through a form <form name=‘myForm’ onSubmit=‘process_form( this ) > ECA 225 Applied Interactive Programming

  3. this keyword cont . . . function process_form( input ){ var first = input.first_name.value; var last = input.last_name.value; } • compared to function process_form( ){ var first = document.myForm.first_name.value; var last = document.myForm.last_name.value; } ECA 225 Applied Interactive Programming

  4. String( ) object • creating a String( ) object • a var which has been assigned a string has access to all String properties and methods var greeting = new String( “Hello” ); var greeting = “Hello”; ECA 225 Applied Interactive Programming

  5. String( ) properties • to access a String( ) property use dot notation var greeting = “Hello”; alert( greeting.length ); ECA 225 Applied Interactive Programming

  6. String( ) methods ECA 225 Applied Interactive Programming

  7. String( ) methods cont … ECA 225 Applied Interactive Programming

  8. String( ) methods cont … ECA 225 Applied Interactive Programming

  9. String( ) methods cont … • to use a String( )method use dot notation • all methods return a value of some kind • most of the time, what’s returned is some version of the original string • use of String( ) methods do not change the value of the original string • to save what is returned, assign it to a var ECA 225 Applied Interactive Programming

  10. string.toUpperCase( ) • converts all the characters in the string to uppercase var greeting = “Hello”;var result = greeting.toUpperCase( ); // returns HELLO ECA 225 Applied Interactive Programming

  11. string.toLowerCase( ) • converts all the characters in the string to lowercase var greeting = “Hello”;var result = greeting.toLowerCase( ); // returns hello ECA 225 Applied Interactive Programming

  12. string.indexOf( ) • determines if one string is contained by another • index values are zero based • returns the index of the character in the larger string where the smaller string begins • if no match occurs, -1 is returns • to check for a simple match, test whether the returned value is something other than –1 • optional 2nd parameter, the starting index of the search ECA 225 Applied Interactive Programming

  13. string.indexOf( ) cont … var greeting = “Hello my Baby.”; var result = greeting.indexOf(“Hello”);alert( greeting );alert ( result ); • change argument to “llo” • change argument to “ool” ECA 225 Applied Interactive Programming

  14. string.charAt( ) • extracts a single character at a known position • pass the method an index number as an argument • returns the character at the index number var greeting = “Hello my Baby.”; var letter = greeting.charAt( 0 ); alert ( letter ); ECA 225 Applied Interactive Programming

  15. string.charAt( ) cont … • print the string using • for loop • length property • charAt( ) var greeting = “Hello my Baby.”; for( i=0; i<greeting.length; i++ ){ document.write(greeting.charAt( i )) } ECA 225 Applied Interactive Programming

  16. string.substring( ) cont … • extracts a contiguous string of characters • takes 2 parameters • beginning index • ending index ( not part of the substring ) var greeting = “Hello my Baby.”; var result = greeting.substring( 0,4 ); alert( result ); ECA 225 Applied Interactive Programming

  17. string.substr( ) cont … • extracts a contiguous string of characters • takes 2 parameters • beginning index • length of the substring var greeting = “Hello my Baby.”; var result = greeting.substr( 9,4 ); alert( result ); ECA 225 Applied Interactive Programming

  18. form validation • form input validation using charAt( ) function valZip( ){ var zip = document.form1.zip.value; if( zip.length != 5 ) { alert( “Invalid zip code") } for( i=0; i < zip.length; i++) { if( zip.charAt( i ) < 0 || zip.charAt( i ) > 9 ) { alert( “Invalid zip code") } } ECA 225 Applied Interactive Programming

More Related