Updated 07/18/14 |
|
Using the math Object in your JavaScriptsIn this section we'll start with a few simple examples of a statement that uses the math object. In this example I am going to display the math objects PI propery. I'll write a simple function that will return the value of pie. Then I'll use the document.write() method to display the value that is returned by the getPi() function in the following script: <html><head><title>Get Pie</title> { The above lines of code are displayed on a web browser like this: You can also use use the Math object with methods as shown in my next example, here is the code: <html><head><title>Round numbers</title> This is a simple script that will round off a a number to the closest integer. For this example we use the number 7.8 which is rounded off. So if you used the above script your browser would display the number 8. Now I'm going to demonstrate using a script that shows a random image along with its caption when the users web browser is refreshed. This was a script I wrote for my brothers website Black Entertainment Blogs. There are several items of inscribed poetry on this website and he wanted a random item to display every time the pages is entered. I created and array of the products and use the Math method random to select a random product and appropriate matching text each time the web page is accessed or the page is refreshed. Here is what the code looks like on a web brower the code and explanation will follow the script.
* Note: in the above script there are 9 items in the array the extra items in the array are omitted for spacing purposes. <html><head> function randomItemDisplay() Go ahead and hit the refresh button on your browser's tool bar or by selecting refresh from the view menu, you should see a different item from the saleItem array every time.
Now you can try creating your own JavaScript using the Math object. If you need help with this or any other script contact me at webmaster@cayemay.com |
This site was last updated 07/16/14