3. Working with Strings

Download Report

Transcript 3. Working with Strings

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
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 Functions
Math object
•
•
•
•
Math.pow(x,y) = x ^ y
Math.sqrt(x) = square root of x
Math.round(x)
All these functions are
asking functions
And many others
– Math.random()
– Math.min(x,y)
– Math.abs(x)
– Math.cos(x)
– Math.exp(a)
- Math.max(x,y)
- Math.floor(x)
- Math.sin(x)
- Math.tan(x)
Example: Euclidean Distance
• Write a function the computes the Euclidean
distance between 2 points in 2D.
• The formula for distance is:
d  ( x1  x 2)  ( y1  y 2)
2
• Test your function
2
Example: Manhattan Distance
• Write a function the computes the Manhattan
distance between 2 points in 2D.
• The formula for distance is:
d  x1  x2  y1  y2
• 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.