Function examples

So far, this column has focused on learning the basics of programming, so most of the examples are fairly simple text-formatting functions. However, as your knowledge of JavaScript increases, you will learn that functions aren't merely useful, but essential to more complicated scripts.

The first example, cube.htm, illustrates how functions can be called multiple times in a script. Although this example is fairly simple, it should be easy to see how the concept is important. The script itself has a function that takes a number as an argument, and then returns the cube of the number.
See the example, or view the source.


This next example is funcexec, short for function execution example. Like last month's loopexec, it traces the program execution of cube.htm. It is a good example to solidify your understanding of functions.
See the example.


The LED example is probably less useful than even the bwords.htm function, but it illustrates the use of arrays, as well as functions. The program prompts the user for his or her name, and then prints the phrase, "EAT AT (name)'S" in psuedo-LED like letters.
See the example, or view the source.


When trying to think of what to do next, all I could think was, "Travis, whats-a-ever it is, it gots to be funky." The funkify function prints a line of text to the screen, with each successive letter varying in size. Granted, London Bridge is falling down is not exactly the Mothership Connection, but busting the funky JavaScript is easier said than done.
See the example, or view the source.


Bwords.htm is an interesting example, but caution should be exercised when using the function. The function in the program prints every fourth word sdrawkcab (that's backwards -- see what I mean?).
See the example, or view the source.


The final example, rainbow.htm, is my attempt to bring a little more happiness to the Web. Rainbow.htm, as the name implies, takes a line of text and regurgitates it in rainbow colours.
See the example, or view the source.