1 / 14

ITBP 119 Algorithms and Problem Solving

ITBP 119 Algorithms and Problem Solving. Section 2.5 computing with strings Section 2.6 string methods and functions. outline. Review Exercise (event/action) More Exercises on Functions Computing with strings Length property toUpperCase method toLowerCase method Replace method

ocean
Télécharger la présentation

ITBP 119 Algorithms and Problem Solving

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. ITBP 119Algorithms and Problem Solving Section 2.5 computing with strings Section 2.6 string methods and functions

  2. outline • Review Exercise (event/action) • More Exercises on Functions • Computing with strings • Length property • toUpperCase method • toLowerCase method • Replace method • Substring method • Working with numbers • parseInt(str) function • parseFloat(str) function • toString(radix) function

  3. Exercise 12 (section 2.4) • Add to your existing file the ability to change the background color of the page by modifying the bgColor property of the body tag. However you should provide a double click event handler (ondblclick) instead of a single click (onclick). (Note the capitalization which is inconsistent with conventions used elsewhere.)

  4. More Built-in FunctionsMath object • Math.pow(x,y) = x ^ y • Math.sqrt(x) = square root of x • Math.round(x) • And many others • Math.random() - Math.max(x,y) • Math.min(x,y) - Math.floor(x) • Math.abs(x) - Math.sin(x) • Math.cos(x) - Math.tan(x) • Math.exp(a) All these functions are asking functions

  5. Example: Euclidean Distance • Write a function the computes the Euclidean distance between 2 points in 2D. • The formula for distance is: • Test your function

  6. Example: Manhattan Distance • Write a function the computes the Manhattan distance between 2 points in 2D. • The formula for distance is: • Test your function

  7. 2.5 Computing with String • String refers to any sequence of characters. • Example: • var str1 = “hello World !!!” ; • var phone = “00971-3-713-5584” ;

  8. Strings are Objects • Properties: • Length • Actions/methods: • String toUpperCase(): returns an upper case copy of the string • String toLowerCase(): returns a lower case copy of the string • String Replace(from character, to character). • String Substring(start index, end index) All these methods are asking functions

  9. 3 parts of any Computation input output Process

  10. Examples • Read a sentence from the keyboard • Display how many characters the sentence has • Display the sentence in lower case • Display the sentence in upper case • Display the sentence such that you replace all spaces with dashes. Prompt() alert Compute upper case Computer lower case replace….

  11. Example: safer passwords • Write a function that makes your password safer as follows: • Covert a/A  @ • Convert s/S  $ • Convert o/O 0 • Convert g/G 8 • Convert i/I ! • Test your function.

  12. Example: init Caps Names • Write a function that takes a name and after it converts the first letter of the name to capital letter, it returns the new name • Test your function.

  13. Example: title property of the document object • Write a script such that whenever you double clicked on the HTML page, the current title of the page is displayed and then new title is entered. At all times, the new title should be in upper case.

  14. Working with Numbers • parseInt ( str , radix): convert a string to a number. • parseFloat(str, radix): covert a string to a number. • toString(radix): convert decimal to another radix-base number.

More Related