Assignment 3

Back to # 3- Variables and Values Review Questions Back to Main Menu

1. Open your blank web page document and resave it as Assign3-1.htm. Your first task is to use two methods to assign a value to a variable. Inside the <BODY>...<BODY> section of the web page, use the <SCRIPT> tags to enclose some JavaScript statements. Declare a variable named 'info' using the var keyword. Then use an assignment ( = ) to store the value 25 in the variable. Use an alert box to display the value of info by using this statement:

alert("Variable info is " + info);

Then use the prompt method to ask the user to input another value for info, such as their age

info = prompt("Enter your age:","");

and then another alert box to show the changed value. Run your script in both IE and Netscape.

Finally, use the operator 'typeof' to determine the data type of the variable info. typeof will tell you whether the value stored in info is of type number, string, boolean, object, or undefined. It is used like this:

alert(typeof info);

or

document.write( typeof info );

2. Open your blank web page document again and resave it as Assign3-2.htm. This will create a simple measurement converter to change inches to centimeters.

Add some <FORM> tags to your web page's <BODY> section and inside the form, make a text box and a button. Name the text box'inches' and give the button the value "Convert to centimeters''. The text next to the text box should be "Enter the number of inches".

Now inside the tag for the button, you need to add the code to calculate the centimeters. You need to remember that the value in the text box will be stored as string, so the numeric value will be 'parseFloat(inches.value)' and you must multiply by 2.54. So add this code inside the tag for the button:

onClick = "alert( (parseFloat(inches.value) * 2.54) + 'Cm');"

View this in your browser, and debug it so it works correctly.

Return to Top of Page | Back to Main Menu