COSC 106 A, Web Programming Exam 3 Study Guide Answers
Tom Linton, Spring 2004
The exam will cover chapters 9,10, and 11 of our text. If you can do most of the following problems in a timely fashion, with little help from the book, you should be well prepared for the exam.
Write the JavaScript fragments (just what is asked for, no surrounding tags needed) described below:
Write a JavaScript function named newBackground, which has
three input parameters named red, green, and blue. The body of the function
should set the background color of the web-page equal to the result of
concatenating the three input parameters (so the function call
newBackground("AA"," 08"," F6") would set the background color to #AA08F6).
function newBackground(red, green, blue)
{
document.bgColor = "#" + red + green + blue;
}
Assuming that the source document has a form named formy,
with one textbox named fahrenheitBox, and another named celsiusBox, write
the JavaScript fragment that takes the value in fahrenheitBox, converts it
to Celsius (use C = (F -32)*(5 / 9) to convert Fahrenheit temperatures to
Celsius), displays the result in celsiusBox, and then places the focus on
fahrenheitBox and selects the text displayed in fahrenheitBox.
with (document.formy)
{
var f = parseFloat(fahrenheitBox.value);
var c = (f - 32)*(5 / 9);
celsiusBox.value = c;
fahrenheitBox.focus();
fahrenheitBox.select();
}
Write a JavaScript function named randomScore which returns
a random integer from 50 to 100 (no input parameters).
function randomScore()
{
var score = Math.random()*51;
score = Math.floor(score) + 50;
return score;
}
Change the source of the third image on the page to be the
file batman.jpg (which is stored in the same folder as the source code
document).
document.images[2].src = batman.jpg;
Round down the (number) variable named x, and multiply the
result by the (number) variable y to the third power (storing the result in
the variable x).
x = Math.floor(x)*Math.pow(y,3);
What is the final value of the variable x after the following
script is executed?
<script type="text/javascript"
language="JavaScript">
var x = 11;
x++;
x *= 2 + 6 / 3 + 2;
</script>
x starts out at 11, then 12, the last line is equivalent to x *= 6
(2+(6/3)+2=6), so x=72.
Consider the web-page shown below. It consists of a single form (name it formy) with 3 textboxes and a normal button. It is shown exactly as it would look when first loaded into a browser. Use the names (or ids) ageBox, yearsBox, and resultsBox for the names or ids of the three textboxes.