Solitaire in JavaScript
Volume Number: 16
Issue Number: 6
Column Tag: Web Technologies
by Danny Swarzman
One approach to structuring code with initializers
and lambda expressions
Preface
This article introduces JavaScript. I focus on a feature of the language that gets very
little mention in books: the ability to use a function definition as an expression. This
article provides an example of how such expressions can be used to produce a legible
program.
I hope to provide a hint of JavaScript's power to create an interesting user experience
under 4.x browsers, by presenting a sample program that creates a Peg Solitaire game
in a web page.
Getting started with JavaScript is easy. All you need is a text editor and a browser. But
first, let's review some core JavaScript concepts.
JavaScript and HTML
JavaScript interpreters have been written to run in different environments. This
article discusses Client-Side JavaScript, which runs in a web browser. The
JavaScript code is inside the HTML. It can appear:
• Between a tag.
• Inside an HTML tag as the value of an attribute such as onMouseOver,
onMouseOut, etc. This code is interpreted when the event occurs.
• As the value of the HREF attribute in a link or an area tag. In this case, the
JavaScript must be prefixed with javascript:. This is interpreted when the
user clicks on the link or area.
As the browser interprets the HTML, it creates a hierarchy of objects representing
the various parts of the web page. The imbedded JavaScript can access those objects. At
the top of the hierarchy is the document, corresponding to the web page.
The JavaScript program can manipulate the contents of the page though these objects.
It can change the text that appears in a
tag. The JavaScript can insert text into the input stream that is currently beinginterpreted as HTML.
The
When the browser encounters the