Chapter 16
In this chapter we introduce JavaScript so that students can see another language that is similar to
Java, but not the same. We also use JavaScript to introduce some user interface concepts.
In section 16.1 we explain JavaScript syntax and how it differs from Java.
In section 16.2 we show how to put JavaScript in a Web page. This includes show how to
invoke a JavaScript function.
Students like the immediate feedback and the simplicity of JavaScript. But, it can be frustrating
if things aren’t working. There is no compiler to help you find mistakes.
Additional Items
We don’t cover recursion in the book but you can use the Turtle class to draw a tree like
structure (Figure 17). In the final version of the book classes (available from the publisher’s
Web site) there is a method drawTree that takes a branch length to start. The turtle that you call
the method on goes forward that branch length and then creates to two new turtles and turns each
slightly (-5) and (+5) from the current turtle’s heading. Then it tells each of these new turtles to
drawTree with a slightly smaller branch length. The method stops when the branch length gets