A few days ago brought my son interesting math problems with out of school: The mathematics teacher had taught the children the good old
writing square roots
- a process in which the first binomial formula is used to successively the posts of root determined. The process is now almost completely fallen into oblivion, but is held at Waldorf schools still honored - their pronounced conservatism is thanks.
For me it was a nice experience to replicate the process in the form of JavaScript code - in a way that it can be used for practice. Instead of displaying the entire bill at once, pressing a button, the individual steps. So you can try on the paper the procedure itself and make sure at every step, to have made a mistake.
The program aims to produce the bill, as it is produced on paper, the steps are to be provided with comments, and in a separate area to small additional calculations are carried out - similar to the scratch paper, which is at the written account of need.
I set a time limit of three hours, just so I could meet - in a nostalgic reminder of the good old days when I had to prove myself even more in tests: "You have three hours - the clock is ticking." However, I later spent another hour for a few refactorings. The site
http://ruediger-plantiko.net/wurzeln/
shows the result. A few comments follow on the program design. The HTML page displays nothing but the page layout. She is kept entirely by JavaScript. Also click handler, onload handler, etc. have been transferred to the script file. In order to produce readable, yet short JavaScript, I use the Prototype Framework . The interruption between the individual processing steps, it unfortunately requires that interim results should be kept in global variables. At least these global variables are all declared in one place and at a second site, in a reset () deleted
function again.
button can then switch the loop without complicated
The algorithm consists of nine steps. The first three steps are performed only once. The following six steps are executed as long can be up to no more figures from radicands more brought down. Then, the procedure is completed.
My design addressed these nine steps as
anonymous functions in an array
execute []
so that they can be dynamically accessed and executed via an index. The method
executeNextStep ()
that is called for click on the Next
realize ... case
constructs by the pedometer step is continuously increased, the remainder modulo 6 but as an index to access the execute array functions [1]. order to break out of the loop is recommended to an exception - in the simplest case, as here, simply in the form of a String object, which is then displayed. executeNextStep function () {try {
/ / Steps 0-2 execute only once
/ / Step Increase counter
incrementStepCounter ();
/ / Next step
run if (step \u0026lt;= 2)
[step] (); else / / from Step 3 will be periodically execute [3 + ((step-3)% 6 )](); } catch (e) {
/ / exception (eg STOP sign) and exit procedures
comment without step (\u0026lt;b>, + e + "\u0026lt;/ b>"); process exit ();} } / / Here are the steps: execute = [ function () { comment (divide the number to the left in groups of two);
var r = cube root + "," i;
for (i = r.length-2, i> =- 1, i-= 2) {
if (i> = 0)
groups.unshift (r.substring (i, i +2));
else
groups.unshift (r.substring (0.1));}
s = "";
groups.each (function (g)
{s + = '\u0026lt;span class = "group"> '+ g +' \u0026lt;/ span> ';}
);
write (s);}
,
function () {
...
},
...
];
This way of solving the problem, can be safely used in other contexts: namely, whenever an algorithm is to be performed piecemeal, such as here with a loop termination condition. Access to functions as an array element is a sequence of steps
obvious than the access via the function name, such as
self ["step2"] ()
that would be alternatively possible. The Solution is particularly compact as a switch ... case ...
design, without sacrificing readability. On the contrary end, just the hard-to acting
's case, the break in C-like languages (like JavaScript), but also every case
a
have to make it more difficult, in my view, the readability of code. In retrospect, I found it unsatisfactory that I had to spend relatively quite for my Spartan UI design time. For the very logic of the root pulling it took only about half the time I had given me. expect to be fact, that for the problem domain
must spend most of the time, not for the user interface . This was in my safe to do with the fact that I create do not like a full-time web designer constantly new HTML / CSS interfaces, but at once created designs very long and very frugal hold on, because professionally I program mainly business logic and modify your mostly existing designs of user interfaces. Overall, I must repeat, however, my criticism of the evil table layout that I CSS in its current form for the design of the presentation layer simply not find enough grip. [1] In the name
execute the Functional Array I am the way, came after some testing: Expressions like
execute [step] () win through him to read.
0 comments:
Post a Comment