Download presentation
Presentation is loading. Please wait.
Published byMelinda Hardy Modified over 9 years ago
1
More on Objects
2
Goals By the end of this unit you should … … be able to identify selected String properties … be able to identify and use selected String methods … be able to identify selected Number properties … be able to identify and use selected Number methods … be able to identify and use methods and properties of the Math object
3
Quick Review … What is an object? What is a primitive object? What is a compound object? What is a property? What is the difference between a property and a property’s value? What is a method? What is an event?
4
Creating Strings There are two ways to create Strings: ◦ To create a string object, we could use a constructor: var userName = new String(“Waldo”); ◦ Or, we could just assign a string value to a variable: var userName = “Waldo”; I N341, I would like you to use constructor methods.
5
String.length Property The String.length property can be used to return the length of a string (number of characters used): Syntax: String.length Example: var myString = new String(“JavaScript”); var strLength = new Number(0); strLength = myString.length; In the example above, the variable strLength gets the value of 10.
6
String.charAt(index) Method The String.charAt(index) method will return the character at a given index in a string. Indexes are ordinal numbers that indicate a position: 0123456789 <= Index JavaScript <= String
7
Example of String.charAt(index) Example: var myString = new String(“JavaScript”); var returnChar1 = new String(“”); var returnChar2 = new String(“”); returnChar1 = myString.charAt(0); returnChar1 = myString.charAt(4); returnChar1 gets the string value “J” returnChar2 gets the string value “S”
8
String.indexOf(searchValue) Method The String.indexOf(searchValue) method returns the index where a specified value begins in a given string The method takes a string value for its argument and returns an integer value. The method returns –1 if the term is not found.
9
Example of String.indexOf(substr) Example: var myString = new String(“JavaScript”); var search1 = new Number(0); var search2 = new Number(0); search1 = myString.indexOf(“Script”); search2 = myString.indexOf(“Visual Basic”); search1 gets the integer value 4 search2 gets the integer value -1
10
String.concat(moreStrings) Method The String.concat(moreStrings) combines a string value with other string values to make a new String Example: var str1 = new String(“JavaScript ”); var str2 = new String(“is ”); var str3 = new String(“cool.”); var str4 = new String(“”); str4 = str1.concat(str2, str3); str4 gets the value “JavaScript is cool.”
11
String.substr(start, length) Method The String.substr(start, length) method returns a sub-string of characters beginning at a specified index (start) and returning a specified number of characters (length). Example: var myString = new String(“JavaScript”); var rtnSub1 = new String(“”); var rtnSub2 = new String(“”); rtnSub1 = myString.substr(0,4); rtnSub2 = myString.substr(5,3); returnSub1 would be given the value “Java” and returnSub2 would be given the value “cri”.
12
String.substring(index1, index2) Method The String.substring(index1, index2) method returns a sub-string of characters beginning at a specified index (index1) and ending at a specified index (index2). Example: var myString = new String(“JavaScript”); var rtnSub = new String(“”); rtnSub = myString.substring(4,9); returnSub gets the value “Script”.
13
String.toUpperCase() & String.toLowerCase() Methods The String.toUpperCase() and String.toLowerCase() methods convert a given string to upper- or lower-case. Useful for non-case sensitive comparisons. Examples: var myString = new String(“JavaScript”); var rtnUpper = new String(“”); var rtnLower = new String(“”); rtnUpper = myString.toUpperCase(); rtnLower = myString.toLowerCase(); rtnUpper gets the value “JAVASCRIPT” and rtnLower gets the value “javascript”.
14
Number Types JavaScript doesn't distinguish among the different number types. Although there are methods to convert to integer and to a floating point number, we create each using the same constructor: var myInteger = new Number(5150); var myFloat = new Number(57.23);
15
Number.toFixed() Method Number.toFixed(fractionalDigit s) returns a number with a specified number of decimal places, after rounding the given value. Rounding follows typical rounding rules: ◦ If a digit is >= 5, then we round up ◦ If a digit is < 5, then we round down
16
Number.toFixed() Method - Examples Examples: var myNum1 = new Number(0.124); var myNum2 = new Number(0.126); var rtnNum1 = new Number(0); var rtnNum2 = new Number(0); rtnNum1 = myNum1.toFixed(2); rtnNum2 = myNum2.toFixed(2); rtnNum1 gets the value 0.12 and rtnNum2 gets the value 0.13
17
Number.toString() Method The Number.toString() converts a numeric value to a string type Examples: var myNum = new Number(8675309); var rtnStr = new String(“”); rtnStr = myNum.toString(); rtnStr gets the value “8675309” (STRING type and NOT NUMBER type)
18
Math.PI Property The Math.PI property returns the constant value of Pi You can use Math.PI for calculating the circumference or area of a circle Examples: var radius = new Number(5); var circumference = new Number(0); circumference = 2*Math.PI*radius; circumference gets the calculated circumference of a circle
19
Math.abs() Method The Math.abs() method returns the absolute value of a given value Examples: var myNumber = new Number(-55); var absMyNumber = new Number(0); absMyNumber = Math.abs(myNumber); absMyNumber gets the value 55
20
Math.ceil() Method The Math.ceil() method returns the smallest integer greater than or equal to a number Examples: var myNumber = new Number(45.67); var ceilMyNumber = new Number(0); ceilMyNumber = Math.ceil(myNumber); ceilMyNumber gets the value 46
21
Math.floor() Method The Math.floor() method returns the largest integer less than or equal to a number Examples: var myNumber = new Number(45.67); var floorMyNumber = new Number(0); floorMyNumber = Math.floor(myNumber); floorMyNumber gets the value 45
22
Math.max(a, b) Method The Math.max(a, b) method returns the largest number, given a set of two numbers (a, b) Examples: var x = new Number(14); var y = new Number(8); var myMax = Math.max(x, y); myMax gets the value 14
23
Math.min(a, b) Method The Math.min(a, b) method returns the smallest number, given a set of two numbers (a, b) Examples: var x = new Number(14); var y = new Number(8); var myMin = Math.min(x, y); myMin gets the value 8
24
Math.pow(base, exponent) Method The Math.pow(base, exponent) method returns the result of an exponential expression, given a base and an exponent Examples: var x = new Number(2); var y = new Number(3); var result = Math.pow(x, y); result gets the value 8
25
Math.random() Method The Math.random() method returns a “random” number between 0 and 1 Examples: var myRan = new Number(0); myRan = Math.random(); //returns.4678 myRan = myRan*100; //scale the number myRan = Math.Round(myRan); If Math.random() returns.4678, myRan is multiplied by 100 (46.78) and then rounded (47)
26
Math.sqrt(x) Method The Math.sqrt(x) returns the square root of a number, given any number Examples: var x = new Number(49); var result = new Number(0); result = Math.sqrt(x); result gets the value 7
27
Modal Windows A modal window is a small action window that forces a user to respond to it before continuing in an application. Typical examples of modal windows include the “Save As” window, a window confirming an exit, error windows, etc. In JavaScript, we can call on some methods of the window object to produce modal windows.
28
window.alert() Method The window.alert() method is a way to produce a modal output window. The window.alert() method takes a single string variable or a single string literal for an argument. The string represents the message we wish to output to the user.
29
window.alert() Examples Example 1: var msgToOutput = new String(“”); msgToOutput = “Hello World!”; window.alert(msgToOutput); Example 2: window.alert(“Hello World!”);
30
window.prompt() Method The window.prompt() method is a way to produce a modal input window. The window.prompt() method takes two string variables or a string literals (or a combination of both) for arguments. The first string represents the question we want to ask the user. The second is help text that will appear in line where the user is to type their answer. The window.prompt() method always returns a string value!
31
window.prompt() Example Example 1: var txtQuestion = new String(“”); var txtUserName = new String(“”); var txtDefault = new String(“”); txtQuestion = “What is your name?”; txtDefault = “TYPE YOUR ANSWER HERE.”; txtUserName = window.prompt(txtQuestion, txtDefault);
32
For More Information … See the Core JavaScript Reference: http://www.croczilla.com/~alex/reference/ javascript_ref/ http://www.croczilla.com/~alex/reference/ javascript_ref/
33
Questions?
34
Resources JavaScript: The Definitive Guide, Fourth Edition by David Flanagan (O’Reilly, 2002)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.