Introduction
The goal of this project is to write a JavaScript program that will check your solution to a Sudoku puzzle and verify that it is correct. This is a good intermediate-level programming project. Who knows, if you get hooked on programming this could even be the beginning for writing a program to solve Sudoku puzzles!
Prerequisites
It is assumed that you know how to use a text editor to write an HTML file containing your program, and that you know how to load and run the file in your browser. You will also need to know the basics of how JavaScript functions can be written to work with HTML FORM elements. Specifically, you will be working with HTML TEXTAREA and INPUT elements, and using JavaScript String and Array objects in your functions. If you need help with any of this, read the Introduction section in: ABC's of Programming: Writing a Simple 'Alphabetizer' with JavaScript.
New Material
In this project, you will learn some important methods of program control logic, which you will use again and again as you write more programs. These methods are used in just about every programming language (though the actual syntax may vary slightly from language to language). Specifically, you will learn about the JavaScript "for" loop control statements, and "if...else" conditional statements. You will also learn about 2-dimensional arrays (lists of lists).
Writing a JavaScript Program to Check Sudoku Solutions
A Sudoku puzzle consists of a 9 × 9 grid of squares, partly filled in with single-digit numbers. The goal is to fill in all of the squares so that the digits 1–9 appear once and only once in each row, column, and in each of the 3 × 3 sub-squares (colored gray and white). In order to check that a solution is correct, your program will have to verify that each of the numbers 1–9 are used once in each of the nine rows, columns, and sub-squares of the puzzle. To help you get started, we've written four simple JavaScript functions which illustrate how to work with our JavaScript Sudoku FORM:
the ReadCells() function reads each value in the Sudoku form, storing them in a two-dimensional array,
the RestoreCells() function writes these values back to the puzzle (it won't work properly unless you've clicked the "Read Cells" button first),
the ClearPuzzle() function erases all of the entries in the puzzle,
the ClearTextArea() function erases the contents of the TEXTAREA.