JavaScript by Example: JavaScript Core Objects. Pt. 2 | 6
JavaScript by Example: JavaScript Core Objects. Pt. 2
|
|
|---|
A variable called sum is a Function object, created by the Function() constructor. It has two arguments, "a" and "b". The function statements are the last string in the list. These statements will be executed when the function is called. |
|

The with Keyword Revisited
In Chapter 8, we used the with keyword with user-defined objects to make it easier to manipulate the object properties. Recall that any time you reference the object within the block following the keyword, you can use properties and methods without the object name. This saves a lot of typing and reduces the chances of spelling errors, especially when the properties have long names. The String object is used in the following example to demonstrate how with is used.
|
<html><head><title>The with Keyword</title> <h2>Using the <em>with</em> keyword</h2> <script language="JavaScript"> var yourname=prompt("What is your name? ",""); document.write("Welcome " + yourname + " to our planet!!<br>"); document.write("Your name is " + length + " characters in document.write("Your name in uppercase: " + toUpperCase() + |

Using the with keyword to reference an object.
Exercises |
-
Create an array of five animals. Use a for loop to display the values stored there. Now add two more animals to the end of the array and sort the array. (Use JavaScript's built-in array methods.) Display the sorted array.
-
Create an associative array called colors. Each element of the array is a string representing the color, e.g., red or blue. Use the for/in loop to view each element of the array with a color of the font the same color as the value of the array element being displayed.
-
Create a function that will return the current month by its full name. Use the Date object to get the current month. Months are returned as 0 for January, 1 for February, 2 for March, etc. Output should resemble:
The current month is January.
Hint: You can create an array, starting at index 0, and assign a month value to it; e.g., month[0]="January" or use a switch statement, e.g., case 0: return "January". -
An invoice is due and payable in 90 days. Write a function that will display that date.
-
How many days until your birthday? Write a function to calculate it.
-
To calculate the balance on a loan, the following formula is used:
-
PV = PMT * ( 1 - (1 + IR )-NP) / IR
-
PV is the present value of the loan; PMT is the regular monthly payment of the loan; IR is the loan's interest rate; NP is the number of payments remaining.
Write a JavaScript statement to represent this formula. -
Using the formula to calculate the loan balance from the last exercise, write a function that will calculate the principle balance left on a loan where the monthly payments are $600, the annual interest rate is 5.5%, and there are 9 years remaining on the loan. Use the toFixed() Number method to format the output.
-
Apply the ceil(), floor(), and round() methods to the number 125.5567 and display the results.
- Create an array of 10 fortune cookies that will be randomly displayed each time the user reloads the page.
-
Create a string prototype that can be used to create an italic, Verdana font, point size 26.
-
Calculate the circumferance of a circle using the Math object.
- Write a JavaScript program that uses the Array and Math objects. Create an array of 5 sayings, for example: "A stitch in time saves 9", or "Too many cooks spoil the broth". Each time the Web page is visited, print a random saying.
1. Creating a multidimensional array (i.e., an array with more than one index) is not officially supported by JavaScript, but can be simulated with some trickery.
Created: March 27, 2003
Revised: November 19, 2003
URL: http://webreference.com/programming/java_core/2

Find a programming school near you