Cedant web hosting - 115Chapter 10Strings, Math, and Datesstring.substring()method. For the second
115Chapter 10Strings, Math, and Datesstring.substring()method. For the second parameter, the lengthproperty of the stringprovides a basis for the ending character s index value (one more than the actual characterneeded). var stringA = The United States of America ; var excerpt = stringA.substring(stringA.indexOf( ) + 1, stringA.length); // result: excerpt = United States of America Creating statements like this one is not something you are likely to enjoy over and over again, so in Chapter 27 I show you how to create your own library of string functions you can reusein all of your scripts that need their string-handling facilities. More powerful string-matchingfacilities are built into today s browsers by way of regular expressions (see Chapter 27 andChapter 42 on the CD-ROM). The Math ObjectJavaScript provides ample facilities for math far more than most scripters who don t have abackground in computer science and math will use in a lifetime. But every genuine program- ming language needs these powers to accommodate clever programmers who can make win- dows fly in circles on the screen. The Mathobject contains all of these powers. This object is unlike most of the other objectsin JavaScript in that you don t generate copies of the object to use. Instead your scripts sum- mon a single Mathobject s properties and methods. (One Mathobject actually occurs perwindow or frame, but this has no impact whatsoever on your scripts.) Programmers call thiskind of fixed object a staticobject. That Mathobject (with an uppercase M) is part of the refer- ence to the property or method. Properties of the Mathobject are constant values, such as piand the square root of two: var piValue = Math.PI; var rootOfTwo = Math.SQRT2; Mathobject methods cover a wide range of trigonometric functions and other math functionsthat work on numeric values already defined in your script. For example, you can find whichof two numbers is the larger: var larger = Math.max(value1, value2); Or you can raise one number to a power of 10: var result = Math.pow(value1, 10); More common, perhaps, is the method that rounds a value to the nearest integer value: var result = Math.round(value1); Another common request of the Mathobject is a random number. The Math.random() method returns a floating-point number between 0 and 1. If you design a script to act like acard game, you need random integers between 1 and 52; for dice, the range is 1 to 6 per die. To generate a random integer between zero and any top value, use the following formula: Math.floor(Math.random() * (n + 1)) where nis the top number. (Math.floorreturns the integer part of any floating-point num- ber.) To generate random numbers between 1 and any higher number, use this formula: Math.floor(Math.random() * n) + 1
Note: In case you are looking for affordable webhost to host and run your web application check Vision cheap hosting services