Midterm Review
This review doesn't necessarily have all the topics covered
on the midterm, but it does cover most of what will be on the
exam. If you know this material, then you should be set. Also
keep in mind that there are often several different ways to
accomplish the same goal in programming - so you could come
up with a different, but equally valid answer.
Show all answers
Hide all answers
HTML basics
- Write a proper HTML 5 template as demonstarted in class.
Include a documentation comment with the course name and
links to external CSS and JavaSCript files.
Answer
<!DOCTYPE html>
<html lang="en">
<head>
<title>My title</title>
<meta charset="utf-8" />
<link rel="style" href="css/style1.css" />
<script src="js/script1.js"></script>
</head>
<body>
</body>
</html>
JavaScript basics
- Write the HTML code needed to include the external JavaScript code contained in the file: library.js.
Answer
<script src="library.js"></script>
- Write the HTML code needed for containing JavaScript code inside a web page.
Answer
<script>
// the code would go here
</script>
- Write a one line JavaScript comment containing the text: CIS 119.
Answer
// CIS 119
- Write a block (multiline) JavaScript comment containing the text CIS 119
on one line and the text JavaScript Programming on the second line.
Answer
/*
CIS 119
JavaScript Programming
*/
JavaScript I/O
- Write a JavaScript statement that will pop up a window that says, "Hello".
Answer
alert("Hello");
- Write a JavaScript statement that will pop up a window that asks the
user what their name is and leaves them a text box to type it in.
Store the response in a variable named nm.
Answer
var nm = prompt("What is your name?");
- Write a JavaScript statement that will ask the user if it is OK to continue,
and gives them a choice of OK or Cancel. Store the response in a variable
named response.
Answer
var response = confirm("OK to continue?");
- Write a JavaScript statement that will write the following HTML to the current document:
<h1>My Page</h1>
Answer
document.writeln('<h1>My Page</h1>');
- Write a JavaScript statement that will write "Hello, world!" to the console.
Answer
console.log("Hello, world!");
Events and event handling
- The event that is used to detect when the cursor is placed
over an element is ________.
Answer
onmouseover
- The event that is used to detect when the cursor is removed
from being placed over an element is ________.
Answer
onmouseout
- The event that is used to detect when a form's data is
being sent to a server is ________.
Answer
onsubmit
- The event that is used to detect when a web page has finished
loading is ________.
Answer
onload
- The event that is used to detect when an element has been
clicked on by the mouse is ________.
Answer
onclick
- How can you prevent a form from carrying out its action?
Answer
Return false from the form's onsubmit event handler.
- How can you prevent a link from going to its destination?
Answer
Return false from the link's onclick event handler.
- Write the JavaScript statement which will cause the function init
to run when the page is done loading.
Answer
window.onload = init;
DOM manipulation
- Write the JavaScript needed to create a new div element.
Answer
document.createElement('div');
- Write the JavaScript needed to create a new text node containing the text Red Dog.
Answer
document.createTextNode("Red Dog");
- Write the JavaScript needed to append a child node to another node. Assume the parent
node is referred to by the variable par and the child node is
referred to by the variable chld.
Answer
par.appendChild(chld);
Functions
- Write the code needed to call the function doit in five seconds.
Answer
setTimeout(doit, 5000);
- Write a function named display which takes two arguments,
and displays the sum of the two arguments in a popup dialog.
Answer
function display(a, b) { alert(a + b); }
Miscellaneous
- Write the JavaScript code to open a new window whose contents will be the
page: http://www.dilbert.com/
Answer
window.open("http://www.dilbert.com/");
- Write the JavaScript code to change the web page being displayed in the
current window to: http://xkcd.com/
Answer
window.location = "http://xkcd.com/";
Using specific page elements
- Write the code needed to get a reference to a form with the id form2, and
store the reference in a variable named custinfo.
Answer
var custinfo = document.getElementById('form2');
- Write the code needed to set the color of the font for an element with the
id vital to #ff0000.
Answer
document.getElementById('vital').style.color = '#ff0000';
- Write the code needed to set the image displayed in an img element with the
id pic to the image "images/mydog.jpg".
Answer
document.getElementById('pic').src = 'images/mydog.jpg';
- Write the code needed to get an array of references to all of the img
elements on a web page.
Answer
document.getElementsByTagName('img')
- Write the code needed to display a popup dialog box which contains the value
of an input text element with the id addr. The form that the
input text element is in has the id rspForm.
Answer
alert(document.getElementById('addr').value)
- Write the JavaScript code needed to set the background color of the body element
to red (assume the body element does NOT have an id in this case).
Answer
document.getElementsByTagName('body')[0].style.backgroundColor = '#ff0000';
Arrays
- Write the JavaScript code needed to create a new array with the name
names.
Answer
var names = new Array();
- Write the JavaScript code needed to create a new array with the name
odds that initially stores the numbers: 1, 3, 5, 7, 9.
Answer
var odds = new Array(1, 3, 5, 7, 9);
- Write the JavaScript code needed to store the string "Hilda" in
the first element of the array named names.
Answer
names[0] = "Hilda";
- Write a loop that will display all the elements of an array named
arrayBaby in the current document, with each element in their own
HTML div element.
Answer
for (int i=0; i<arrayBaby.length; i++) {
dodument.writeln("<div>" + arrayBaby[i] + "</div>");
}
- Write the JavaScript statement needed to break the string "12:34:56:78"
into tokens delimited by the colon and store the four tokens in an array
named nums.
Answer
var nums = "12:34:56:78".split(":");
- What will the following expression evaluate to:
"Sun,Mon,Tue,Wed,Thu,Fri,Sat".split(",").length
Answer
7
- What will the following expression evaluate to:
"Sun,Mon,Tue,Wed,Thu,Fri,Sat".split(",")[4]
Answer
Thu