Recursion examples

As this section of the tutorial was focused more on solidifying existing knowledge than on introducing new concepts, there are fewer examples. However, the fade example is by far the largest, and most informationally rich, example to appear so far.


This first example, fact.htm, is an example of a recursive function. The code takes the factorial of the first 10 integers and prints them to the screen.
See the example or view the source.


The next example, crash.htm, demonstrates one of the potential pitfalls of using recursive functions. The code itself is an unbridled version of fact.htm; which sets up an infinate loop and calculates the factorial of the counter until the browser chokes. While this script won't crash most browsers altogether, I have seen it take down browsers completely, do don't say I didn't warn you!
See the example or view the source.


For some time now, I've felt an inexplicable pressure to include a script that fades into a background colour. Well, I've not only done that, but more. Fade.htm dynamically fades from the current background colour to a new colour.

The script is longer than some of the previous examples, so I've structured the presentation as an example of top-down programming. The example begins with the task to be implemented, and breaks it down into all of its component parts. Each of the simple tasks are then implemented, and then assembled into the final program. To get the most out of the example, follow each link in the examples, and view the source code to each seperate component.
View example.