CSCI 242 Exam 1 -- Open Section

You are strongly encouraged to save your answers to a file which you can uploaded to Moodle submission page for Exam 1. However, you can also write your answers on lined paper and turn them in at the end of the period.

Many of these questions are similar to homework problems. You are welcome to consult your homework in writing your answers.

If you add up the points for the closed and open sections of the exam, you will get 80. You are being given a 20-point head start.

Question 1 (10 points)

Suppose the following paragraph appears in an HTML file.

<p id="ChangeMe">Paragraph text</p>

Subquestion 1A

Write some CSS that would make the letters of the paragraph blue. Hint: color.

#ChangeMe { color: blue; }

Subquestion 1B

Write a section of JavaScript code that will change the text of this paragraph using the DOM element.

var chElement = document.getElementById("ChangeMe");
chElement.innerHTML = "Changed Text!";

Subquestion 1C

Write a section of JavaScript code that will change the color of the text of this paragraph using the appropriate DOM element.

var chElement = document.getElementById("ChangeMe"); = "red";

Question 2 (8 points)

Explain how you would change the background color of a web page every 10 seconds using JavaScript. You don’t have to write the actual code. Just put in enough detail to convince a potential employer (or present instructor) you’d be able to do this.

First, write a function that changes the background color. Then invoke JavaScript’s setInterval to tell JavaScript to call the background change function every 10 seconds.

Question 3 (8 points)

For an array V, write a JavaScript for loop that counts the number of elements of V that are Strings. Hint: Consider using the for ... in with the typeof operator.

count = 0 ;
for (x in V) {
  if (typeof V[x] == "string") {

Question 4 (8 points)

In Chapter 1 of the textbook, the document.write method was used to fill the body of a page that contained only a JavaScript program. (See the annoying energy drink page for an example of this.)

Write a similar JavaScript loop to display the first 1000 square numbers. The lines of your program’s output should start with something like the following.

1 squared is 1.
2 squared is 4.
3 squared is 9.
4 squared is 16.
for (var i = 1; i<=1000; i++) {
  document.write(i + " squared is " + (i*i) + "<br/>") ;

Question 5 (10 points)

We ve also seen programs, such as the textbook’s playlist page and its associated playlist JavaScript code where items are added to a list.

Assume that a web page, as loaded, contains the outline of a table as shown below:

<ul id="squares">

What JavaScript code is required to add the 1000 squares as items in a list? The start of the generated list would resemble the list shown below.

var ulElement = document.getElementById("squares") ;
for (var i = 1; i<=1000; i++) {
  var liElement = document.createElement("li") ;
  liElement.innerElement = i + " squared is " + (i*i) ;
  ulElement.appendChild(liElement) ;