Edited at 2005/07/08 08:58:56 PST
Homework
-
Read chapters 12 and 13
-
Do Prelabs for 12 and 13
-
Listen to the webcast lecture about using the UC Berkeley library search engines
-
Find 2-3 books via the UC Berkeley library search
-
NOTE: Remember to bring your IDs (yellow cards) on friday, so you can have access to the library.
Prelab Answers
Chapter 10 Review Questions
6. formal parameters, actual parameters
7. d
Chapter 10 Exercises
10.2 Write the HTML code that would create a form with four textboxes, displaying them with two on the first line and two on the second line. Give them each a width of 15 characters. Each box should initially appear with the text “Box 1” (or “Box 2”, etc.) in it, except the last one, which should be empty.
<form id="sampleForm" name="sampleForm">
<input type="text" name="firstBox" value="Box 1" size="15" />
<input type="text" name="secondBox" value="Box 2" /><br />
<input type="text" name="thirdBox" value="Box 3" />
<input type="text" name="fourthBox" />
</form>
10.6 For each of the following cases, write the code for a function named processResults:
(a) The function receives the value of a textbox and stores it in a formal parameter named theMessage. The code in the function copies the value of the textbox into another textbox named outputBox that belongs to a form named messageDisplay.
(b) The function receives two textbox objects themselves and stores them in formal parameters named box1 and box2. The code in the function copies the value of box1 into box2.
(a)
function processResults(theMessage) {
document.messageDisplay.outputBox.value = theMessage
}
(b)
function processResults(box1, box2) {
box2.value = box1.value
}
10.7 Repeat Problem 10.6 for the following cases:
(a) The function receives the value of a textbox and stores it in a formal parameter named theMessage. It also receives a textbox object and stores it in a formal parameter named theMessageBox. The code in the function copies the value of theMessage into the textbox represented by the parameter theMessageBox.
(b) The function receives a form and stores it in a formal parameter named theForm. The original form has two textboxes named inputBox and outputBox. The code in the function copies the value of inputBox into the outputBox.
(a)
function processResults(theMessage, theMessageBox) {
theMessageBox.value = theMessage
}
(b)
function processResults(theForm) {
theForm.outputBox.value = theForm.inputBox.value
}
10.10 Write the HTML code for a form with three textboxes named ageBox, genderBox, and schoolyearBox, appropriately labeled. The form should use a submit button to email the information in the boxes to the address studentsurvey@anonymouspollster.org. The form should also implement a reset button.
<form method="post" enctype="text/plain" action="mailto:studentsurvey@anonymouspollster.org">
Please enter your age:<br />
<input type="text" name="ageBox" size="3" /><br />
Please enter your gender (M or F):<br />
<input type="text" name="genderBox" size="1" /><br />
Please enter your school year (Frosh, Soph, Jr, or Sr):<br />
<input type="text" name="schoolyearBox" size="5" /><br />
<input type="submit" value="Submit information" />
<input type="reset" value="Erase info and start over" />
</form>
Chapter 11 Review Questions
1. c
4. a, b, c, and d
5. d
9. c
Chapter 11 Exercises
11.1
(a) A loosely typed programming language is a language that allows a given variable to store different types of values at different times. For example, at one point it might contain a number value and at another point a string value. (A strongly typed language does not allow this flexibility—any given variable may only store one type of value.)
(b) A boolean value is a value that is either true or false.
(c) Variable scope refers to where in a script or program a variable may be used. A variable with local scope (a local variable) may only be used inside the function within which it is declared. A variable with global scope (a global variable) may be used anywhere in a source document.
11.2
A local variable is declared inside a function and may only be used inside that function. A global variable is declared outside of a function (but in a <script> element of course) and may be used in any <script> element in that source document. Global variables are convenient because, once declared, they may be used anywhere in the document. But such global use can lead to programming errors in longer programs. So in general it is better to make your variables local ones whenever possible.
11.4
<script type="text/javascript">
function testingVariables() {
var num1 = 19.54
var name3 = "Theodore Roosevelt"
var matchFound = true
num1 = 29.1
name3 = "Abraham Lincoln"
matchFound = false
}
</script>
11.7
<script type="text/javascript">
var luckyNumber = 10*Math.random() //The var keyword is not required
var luckyNumber2 = Math.floor(100*Math.random()) + 1
</script>
11.10
<script type="text/javascript">
var hallOfFamer //Declare global variable (use of var is optional)
function fullName(firstName, lastName) {
var theName //Could also write these two lines as one line:
theName = firstName + " " + lastName // var theName = firstName + " " + lastName
return theName
}
hallOfFamer = fullName("Babe", "Ruth")
</script>