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

  1. Write a proper HTML 5 template as demonstrated 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>
  2. JavaScript basics

  3. Write the HTML code needed to include the external JavaScript code contained in the file: library.js. Answer <script src="library.js"></script>
  4. Write the HTML code needed for containing JavaScript code inside a web page. Answer <script> // the code would go here </script>
  5. Write a one line JavaScript comment containing the text: CIS 119. Answer // CIS 119
  6. 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 */
  7. JavaScript I/O

  8. Write a JavaScript statement that will pop up a window that says, "Hello". Answer alert("Hello");
  9. 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?");
  10. 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?");
  11. 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>');
  12. Write a JavaScript statement that will write "Hello, world!" to the console. Answer console.log("Hello, world!");
  13. Events and event handling

  14. The event that is used to detect when the cursor is placed over an element is ________. Answer onmouseover
  15. The event that is used to detect when the cursor is removed from being placed over an element is ________. Answer onmouseout
  16. The event that is used to detect when a form's data is being sent to a server is ________. Answer onsubmit
  17. The event that is used to detect when a web page has finished loading is ________. Answer onload
  18. The event that is used to detect when an element has been clicked on by the mouse is ________. Answer onclick
  19. How can you prevent a form from carrying out its action? Answer Return false from the form's onsubmit event handler.
  20. How can you prevent a link from going to its destination? Answer Return false from the link's onclick event handler.
  21. Write the JavaScript statement which will cause the function init to run when the page is done loading. Answer window.onload = init;
  22. DOM manipulation

  23. Write the JavaScript needed to create a new div element. Answer document.createElement('div');
  24. Write the JavaScript needed to create a new text node containing the text Red Dog. Answer document.createTextNode("Red Dog");
  25. 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);
  26. Functions

  27. Write the code needed to call the function doit in five seconds. Answer setTimeout(doit, 5000);
  28. 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); }
  29. Miscellaneous

  30. 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/");
  31. 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/";
  32. Using specific page elements

  33. 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');
  34. 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';
  35. 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';
  36. Write the code needed to get an array of references to all of the img elements on a web page. Answer document.getElementsByTagName('img')
  37. 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)
  38. 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';
  39. Arrays

  40. Write the JavaScript code needed to create a new array with the name names. Answer var names = [];
  41. 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);
  42. Write the JavaScript code needed to store the string "Hilda" in the first element of the array named names. Answer names[0] = "Hilda";
  43. 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 (var i=0; i<arrayBaby.length; i++) { document.writeln("<div>" + arrayBaby[i] + "</div>"); }
  44. 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(":");
  45. What will the following expression evaluate to:
    "Sun,Mon,Tue,Wed,Thu,Fri,Sat".split(",").length Answer 7
  46. What will the following expression evaluate to:
    "Sun,Mon,Tue,Wed,Thu,Fri,Sat".split(",")[4] Answer Thu