Chad Lawrence

Web Development for Tech Applications (TECH 64095-001)

clawre13@kent.edu

Chapter 12 - Document Object Model (DOM): Objects and Collections

12.1 State whether each of the following is true or false. If false, explain why.

  1. Every XHTML element in a page is represented by a DOM tree. False. Every element is represented by a DOM node. Each node is a member of the document's DOM tree.
  2. A text node cannot have child nodes. True.
  3. The document node in a DOM tree cannot have child nodes. False. The document is the root node.
  4. You can change an element's style class dynamically with the style property. False. The style class is changed with the className property.
  5. The createElement method creates a new node and inserts it into the document. False. the createElement method creates a node, but does not insert it into the DOM tree.
  6. The setInterval method calls a function repeatedly at a set time interval. True.
  7. The insertBefore method is called on the document object, taking a new node and an existing one to insert the new one before. False. insertBefore is called on the parent.
  8. The most recently started interval is stopped when the clearInterval method is called. False. clearInterval takes an interval identifier as an argument to determine which interval to end.
  9. The collection links contains all the links in a document with specified name or id attributes. False. The links collection contains all links in a document.

12.2 Fill in the blanks in each of the following statements.

  1. The innerHTML property refers to the text inside an element, including XHTML tags.
  2. A document's DOM tree represents all of the nodes in a document, as well as their relationships to each other.
  3. The length property contains the number of elements in a collection.
  4. The item method allows access to an individual element in a collection.
  5. The images collection contains all the img elements on a page.
  6. The history object contains information about the sites that a user previously visited.
  7. CSS properties may be accessed using the style object.

12.8 (15 Puzzle) Write a web page that enables the user to play the game of 15. There is a 4-by-4 board (implemented as an XHTML table) for a total of 16 slots. One of the slots is empty. The other slots are occupied by 15 tiles, randomly numbered from 1 through 15. Any tile next to the currently empty slot can be moved into the currently empty slot by clicking on the tile. Your program should create the board with the tiles out of order. The user's goal is to arrange the tiles in sequencial order row by row. Using the DOM and the onclick event, write a script that allows the user to swap the positions of the open position and an adjacent tile. [Hint: The onclick event should be specified for each table cell.]

12.9 Modify your solution to Exercise 12.8 to determine when the game is over, then prompt the user to determine whether to play again. If so, scramble the numbers using the Math.random method.

12.10 Modify your solution to Exercise 12.9 to use an image that is split into 16 equally sized pieces. Discard one of the pieces and randomly place the other 15 pieces in the XHTML table.

12.8, 12.9, 12.10 Combined Answer

The following link is the above puzzle game without randomizing the squares. This is to test the randomly removed tile on refresh and test the endgame functions without completing the entire puzzle. (Simply move one or two squares, then move them back.)

Puzzle Game without randomized tiles