Meeting 12
Using JavaScript to Control the
Browser
12.1 Homework Discussion
Professor: Wha t did you find out? Is our code operatio nal?
Maria: We did some testing an d it seems all right. At least we didn’t find any er rors.
Professor: How did you test the code?
Mike: First we wanted to have a method that would print the whole Sudoku pu zzle so
we could see what numbers are in it. Heres the c ode:
Sudoku.prototype.print = function() {
var i, j;
document.write("<table border=’1’>");
for (i = 0; i < 9; i++) {
document.write("<tr>");
for (j = 0; j < 9; j++) {
document.write("<td>")
if (this.board[i][j].getNumber() != null) {
document.write(this.board[i][j].getNumber());
}
document.write("</td>")
}
document.write("</tr>");
}
document.write("</table>");
};
The code is nothing special. It simply writes out all the numbers that it finds inside
the puzzle in form of an HTML table.
227
Professor: That’s nice. Wha t else did you do?
Maria: We couldn’t think of any systematic approach that would perform an ex-
haustive test. We simply tried to insert so me wrong numbers that violated only one
of the three conditions. If a number had violated more than one condition, then we
would only have been testing a condition that is violated first inside the setNumber()
method of Sudoku. We did the same test against the initial clues as well as against a
manually entered nu mber. At the end we also tried to enter the same numbe r in the
same cell twice.
This is our cod e:
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 test = [];
var S = new Sudoku();
S.setClues(initial);
test.push(S.setNumber(2, 0, 0));
test.push(S.setNumber(8, 0, 0));
test.push(S.setNumber(9, 0, 3));
test.push(S.setNumber(6, 0, 0));
test.push(S.setNumber(6, 0, 5));
test.push(S.setNumber(6, 3, 0));
test.push(S.setNumber(6, 2, 2));
test.push(S.setNumber(6, 0, 0));
console.log(test);
S.print();
Mike: All except two of the calls to th e setNumber() method retur n false, which
was expected. At the end, the whole puzzle is written to the browser window and you
can see that numbe r six was inde ed successfully inserted to the upper left cell of the
puzzle.
228 Meeting 12. Using JavaScript to Control the Browser
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.103.210