As you can see, the functio n ind eed defin es and returns another, anonymous fun ction.
It also creates clo sure for this anonymous function, insid e which it stores the thre e
passed arguments: a reference to the Sudoku object, and the row and column indexes
of the cell. Note that a sepa rate anonymous function -definition object is created for
each of the text boxes, whose closure stores the row and column indexes of the cell
associated with the current text box .
The anonymous function nested within handleInput() is quite simple. First, it
parses the value currently written in the text box that actually triggered the input event.
Recall that this is a refe rence to a method’s invocation context, which in our case is
the Element object r epresenting the <input> elemen t that triggered the event. After
parsing the value, the function resets the element’s class attribute to answer.
If the entered number is between one and nine inclusive, then the function tries to
insert that number into the puzzle grid using the setNumber() meth od of the Sudoku
object. If the operation is not successful, then the element’s class is changed to wrong
so that th e entere d numb er will be colored red . Otherwise, if ther e is anything othe r
than a number between one and nine entered in the text box, the old nu mber is removed
from the correspo nding Sudoku cell and the element’s class is set to candidate.
However, the entered value is still shown in the text box but is rendered smaller.
The last missing piece of code is the Window’s onl oad event-handler definition,
which incorporates a two-dimensional array containing the initial clue, a Sudoku ob-
ject creation and initialization, and an invocation o f the start() method:
window.onload = function() {
var initial = [
[null, 5, 7, null, 4, null, null, 2, null],
[null, 3, 8, null, null, 2, 7, null, null],
[null, 1, null, 7, 3, null, null, null, null],
[null, 7, null, 2, 8, null, null, null, 3 ],
[3, null, 4, null, null, null, 1, null, 6 ],
[1, null, null, null, 6, 4, null, 7, null],
[null, null, null, null, 2, 7, null, 1, null],
[null, null, 1, 9, null, null, 2, 3, null],
[null, 4, null, null, 1, null, 5, 9, null]
];
var s = new Sudoku();
s.setClues(initial);
s.start("sudoku-puzzle");
}
That’s it. We’re ready to play Sudoku.
13.4. Completing Sudoku Puzzle H elper 261