HTML and CSS Reference
In-Depth Information
To create the checkSolution() function:
1. Return to the hanjie.js file in your text editor.
2. Directly above the drawGrid() function, insert the following code for the
checkSolution() function (see Figure 13-34):
It's very easy to make
a typing mistake when
entering a long condi-
tional expression involving
several conditions. Make
sure that all sets of
parentheses have opening
and closing () symbols,
and that all text strings
are completely enclosed
within quotation marks.
function checkSolution() {
/* Create a collection of all puzzle cells */
var allCells = document.querySelectorAll(“#hanjieGrid td”);
/* Set the initial solved state of the puzzle to true */
var solved = true;
/* Loop through the puzzle cells, exiting when an incorrect
cell is found, setting the solved variable to false */
for (var i = 0; i < allCells.length; i++) {
var cellColor = allCells[i].style.backgroundColor;
var cellClass = allCells[i].className;
/* A cell is incorrect if it is in the marked class and is
not gray
or in the empty class and is not white */
if ((cellClass == “marked” && cellColor !== “rgb(101, 101,
101)”) ||
(cellClass == “empty” && cellColor !== “white”)) {
solved = false;
/* If solved is still true after the loop, display an alert box
if (solved) alert(“Congratulations! You solved the puzzle!”);
Figure 13-34
inserting the checksolution() function
Search WWH ::

Custom Search