Kurt Cagle posted an article about helping his daughter with sums. Since I should be doing marking today, it was clearly imperative to look at the problem and see how I would code it. I'm also working on my SQL tutor so there is some connection I could use to justify this diversion if questioned.
Kurt's code is specific to a sum structure (the range of digits, the size of numbers the number of numbers). This code generalises over these parameters, not merely because I think that makes a more interesting application, but because generalisation often leads to a cleaner design. As ever, it's hard to hit the Goldilocks spot, not too much, not too little. For me it always takes a bit of experimentation - surely I should be able to apply a design technique to hit the sweet spot?
Adding an interface to change the problem specification could be achieved in several ways, including XForms. This version uses a very simple model definition which is used to generate a form or a parameterised URL and to extract values from the URL.
One magic number remains - the 10 of the base. Its tempting to use a global variable to set this property of the problem specification, but I'd rather parameterize the functions. Max base is 10. An obvious extention is to be be able to do hex sums but digits cant be integers then.
Sometimes it appears that the number of rows is less than the number specified. This is because sometimes 0 is generated as the number which is rendered as all spaces.
The function to generate rows from numbers is ugly and over-specific. In fact the styling of the last row to create a line doesn't work when zeros are allowed, and the operator might be parameterised too, but the code to converted to padded cells would be the same.
Refactoring the number-to-row function and using an hr element to divide problem from answer yields a more statisfactory solution: