CSC 112 JavaScript Test #1 Spring 2006
(Test Program with Solutions Implemented)
Name:______________________________________ Monday February 13, 2006
Print this exam and write your answers on the printed copy or type your answers
on the document and print. Turn in the hard copy.
- (40 pts.) Write code for each of the following:
- Create a form named "codelab".
<form name="codeLab">
</form>
- Add text input named "input".
<input type="text" name="input" cols=50>
- Add a textarea named "feedback".
<textarea name="feedBack" cols="50" rows="5">
</textarea>
- Add an object or event handler so the function "analyze" is called
(this function -to be written- takes information from the input and
generates feedback.)
Method 1:
<form name="codeLab" onsubmit="analyze();return
false;">
<input type="submit" value="Analyze Input">
Method 2:
<input type="button" name="b0" value="Read"
onclick="analyze()">
- (30 pts.) Write an entire function definition named "analyze" that
- Checks to see if the input is empty and alerts the user to enter
input if so.
function analyze() {
if(!document["codeLab"]["input"].value)
alert("No Input Data to Analyze");
}
- Checks to see whether the input is more than 10 characters, then
places "Long winded today?" in the feedback.
if(document["codeLab"]["input"].value.length > 10)
document["codeLab"]["feedBack"].value="Long winded today?";
- If the test condition in b. is false, place "concisely
stated" in the feedback.
else
document["codeLab"]["feedBack"].value="Concisely stated.";
- (30 pts.) The script in the head declares an array named "ansKey"
as follows:
var ansKey = new Array();
ansKey[0] = "b"; ansKey[1] = "c"; ansKey[2] = "a";
- What is value of guess (exactly) after the following loop?
var guess = "";
for (i=1;i<ansKey.length;i++){
guess += ansKey[i] + (i%2==0? ".":", ");
}
c, a.
- In the body of an HTML document, a form named "test" has
three radio buttons for each of three questions (nine radio buttons
total). The first three radio buttons are named "q0", the next
three "q1", and finally the last three "q2". The value for
each button corresponds to the displayed question and the id for each
button is "a", "b", and "c" respectively for each question.
Write a function that gets the form and button name (or part of it)
and checks the selected answer for each question against the
ansKey value for each question. When completed the function should call
an alert window that displays the number correct out of three.
var score = 0;
for(questionNumber = 0; questionNumber < ansKey.length; questionNumber++)
{
theButtons = document["test"]["q"+questionNumber];
for(i=0; i < theButtons.length; i++)
if(theButtons[i].checked & theButtons[i].id ==
ansKey[questionNumber])
{
score++;
}
}
alert("You got " + score + " of three correct.");