![](http://images-20200215.ebookreading.net/24/3/3/9781498731454/9781498731454__start-programming-using__9781498731454__bg11c.png)
The first line of this algorithm should find a ll the empty cells in the grid and place them
into a one-dimensional array. Note that you will need the row and column in dexes of
cells to be able to place them into the puzzle grid, which Cell objects don’t store. To
get around this, you can either add row and c olumn indexes to the Cell class object
or simply create an array of pairs of indexes storing positions of empty cells.
Note that Check in the above algorithm is a function, and you will notice a curious
thing: that this function in fact calls itself fr om within the inner most if/else statement.
A function that calls itself is called a recursive function, whic h offers an elegant so -
lution for types of pr oblems like this one. What you have to do is first try to place
number one in the fir st empty cell. If that is not possible, then try it with n umber two,
and number three, and so on, until either you find a pr oper number or you run out of
numbers. If you suc cessfully place a number into the grid, then repeat the whole story
with the next emp ty cell. If, however, no one number ca n be placed into the current
cell, then you should go back to the previous empty cell and try to replace its number
with a bigger one.
If you use a r ecursive fu nction to solve this problem, then lots of bookkeeping is
done automatically for you. Each time the f unction is called, a new function object
is created and appended to the current scope chain. If the function also creates loc al
variables rememb ering the index of th e cell and the number that h as been p la ced most
recently into that cell, then th ere is not much work left for you. Whenever no further
number can be placed in the current cell, the current function stops executing, which
automatically removes it fr om the scope chain and the checking continues with the
previous cell from the exact point where it left it.
I admit that this second part of the assignme nt is quite a challenge, but you shall
try to write a program nonetheless. In case you don’t succeed, there’s a solution in
Appendix A.
Which brings us to the e nd of our course. You guys have been terrific and I hop e you
enjoyed it as much as I did. Take care and keep up the good work!
In this meeting: getElementsByTagName(), getElementsByClassName(),
firstElementChild, firstChild, lastElementChild, Element object, Node
object, Document object, parentN ode, appendChild() , oninp ut, passing a rgu-
ments to event handlers, recursion, inherit, createE lement()
264 Meeting 13. User Interface