140 likes | 293 Views
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
E N D
ITBP 119Algorithms 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 • Substring method • Working with numbers • parseInt(str) function • parseFloat(str) function • toString(radix) function
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.)
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
Example: Euclidean Distance • Write a function the computes the Euclidean distance between 2 points in 2D. • The formula for distance is: • Test your function
Example: Manhattan Distance • Write a function the computes the Manhattan distance between 2 points in 2D. • The formula for distance is: • Test your function
2.5 Computing with String • String refers to any sequence of characters. • Example: • var str1 = “hello World !!!” ; • var phone = “00971-3-713-5584” ;
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
3 parts of any Computation input output Process
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….
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.
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.
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.
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.