IT130 -- Final Exam March 9, 2009 1. (10 points) Explain, to someone that does not understand programming, how static web pages differ from dynamic web pages. What sorts of dynamic effects are possible on web pages? How does JavaScript make them possible? Ans: Whereas static web pages, that use only HTML and CSS do not change their content (until they are edited), pages that include JavaScript can change their content, based on user input, random number generators, or time of day clock. Such new content can include the results of calculations or post new images to replace default images on a page, 2. (5 points) What are source code comments and why are they used? How are they designated in HTML, CSS, and JavaScript? Ans: Source code comments are used to explain how a page works or any special features on a page to persons reading the source code. Here are three classs of comments commonly used: /* CSS comment, also used for JavaScript. */ // JavaScript comment (goes until end of line). 3. (10 points) What are some common mistakes to watch out for in completing projects involving JavaScript like Projects 4, 5, and 6? Ans: 1. Misspelling variable names, including capitalization. 2. HTML tag name or ID doesn't match the variable name. 3. Function name in head does not match function name in onclick statement. 4. Missing end ;, }, , or > symbols. 5. Missing end style or script tags. 6. Variables not initialized correctly. 7. Using = instead of == or vice versa. 8. Radio button subscripts out of range. 9. Image file or target or hyperlink in wrong location. 10. Double version problem: editing one file and testing another. 4. (2 points each) Explain what these items mean. You can use an example in your explanation if you wish. a. Assignment Operator in JavaScript. Ans: The symbol = for assigning a value to a variable. Example: x = 5; assigns the value 5 to the variable x. b. CSS Class Ans: A class is like a style, but it has a user-defined name starting with a dot. Use the style c like this: ...... c. CSS Property Ans: A property is an aspect or attribute of an HTML element that you wish to change with CSS. Here is the CSS syntax: element { property:value } d. External CSS Style Ans: Styles that are kept in a .css file separate from the actual HTML document. The HTML document can use a link tag to specify that a certain external style is to be used. e. Domain Name Service Ans: A service built into the Internet that obtains the IP address of a URL that a browser or other software submits. f. Event Handler Ans: A function specified in an onclick or onload statement. This function then executes when a button is clicked or the page is loaded. g. getElementById JavaScript Function Ans: A function that obtains a control, given its ID. Define a control with ID like this: Then use that control like this: v = document.getElementById(txtInput).value; h. Variable Initialization in JavaScript Ans: To declare a variable and set its value in one line: var x = 5; 5. (2 points each) What does each of these color codes represent? a. rgb(0,255,255) Ans: Cyan or aqua. b. rgb(255,255,0) Ans: Yellow. c. rgb(130,0,0) Ans: Maroon. d. rgb(215,215,215) Ans: Silver or light gray. e. rgb(255,128,0) Ans: Orange. 6. (5 points each) A web page randomly displays one of the images 1.jpg, 2.jpg, or 3.jpg when the page is loaded. It randomly changes the image when a button is clicked. a. Write HTML code for a page that displays the image, the button and a heading for the page. You choose the names for the image, the button, and the form. Don’t forget the onclick attribute for the button and the onload attribute for the body. Ans:
b. Write an event handler for the button that randomly displays one of the images 1.jpg, 2.jpg, or 3.jpg. function showRandomImage( ) { var n = 1 + Math.floor(3 * Math.random( )); if (n == 1) { document.images.imgRandom.src = 1.jpg; } else if (n == 2) { document.images.imgRandom.src = 2.jpg; } else { document.images.imgRandom.src = 3.jpg; } } or function showRandomImage( ) { var n = 1 + Math.floor(3 * Math.random( )); document.images.imgRandom.src = n + .jpg; } c. Write an external style that (i) sets the width of both the image and the button to 1 inch, (ii) sets the font for the heading and the button to bold Georgia, (iii) sets the background color of the body to PaleTurquoise. img, input { width:1in; } h1, input { font-family:Georgia; font-weight:bold; } body { background-color:PaleTurquoise; } 7. (5 points each) Predict the output for each of these code fragments. Explain briefly how you obtained your answer. a. This JavaScript code is supposed to compute the real estate tax for a house (in an unusual way). What is the tax if the house value is $528,193? var v, t; v = parseFloat(document.frmA.txtHouseValue.value); t = 100 * Math.floor(v / 1000); document.frmMain.txtTax.value = t; Ans: 52,810. Here is how to obtain the answer: 100 * Math.floor(v / 1000) == 100 * Math.floor(528193 / 1000) == 100 * Math.floor(528.193) == 100 * 528 == 52800 b. This JavaScript code is supposed to compute the tip for a restaurant meal. What is the tip for a $50.00 meal? var m, t; m = parseFloat(document.frmA.txtMealPrice.value); if (m >= 20.00) { t = m * 0.18; } else { t = m * 0.20; } document.frmMain.txtTip.value = t; Ans: Since the meal is more than $50.00, use the calculation t = m * 0.18 = 50.00 * 0.18 = 9.00. The answer is 9 dollars. c. This JavaScript code Show the table that is produced. document.write(
+ n + | + m + |
Enter your gender: Female Male
9. (15 points) Find as many mistakes as you can: