Java Reference
In-Depth Information
The
String type provides lots of handy functionality for text and gives you ways of fi nding out
how long the text is, searching for text inside the string, and selecting parts of the text.
The
Math type is created automatically and provides a number of mathematical properties
and methods. For example, to obtain a ra¡ndom number between 0 and 1, you use the method
Math.random() .
The
Array type provides ways of manipulating arrays. Some of the things you can do are fi nd
the length of an array, sort its elements, and join two arrays together.
The
Date type provides a way of storing, calculating with, and later accessing dates and times.
JavaScript enables you to create your own types of objects using reference types. These can be
used to model real-world situations and for making code easier to create and more maintainable,
though they do require extra effort at the start.
In the next chapter, you'll turn your attention to the web browser itself and, particularly, the various
objects that it makes available for your JavaScript programming. You'll see that the use of browser
objects is key to creating powerful web pages.
Exercise Questions
Suggested solutions to these questions can be found in Appendix A.
1.
Using the Date type, calculate the date 12 months from now and write this into a web page.
2.
Obtain a list of names from the user, storing each name entered in an array. Keep getting
another name until the user enters nothing. Sort the names in ascending order and then write
them out to the page, with each name on its own line.
3.
In this chapter, you learned about how you can use the pow() method inventively to fi x a
number to a certain number of decimal places. However, there is a fl aw in the function you cre-
ated. A proper fix() function should return 2.1 fi xed to three decimal places like this:
2.100
However, your fix() function instead returns it like this:
2.1
Change the fix() function so that the additional zeros are added where necessary.
Search WWH ::




Custom Search