Functions

Welcome to the Web Cellar, the Internet's premier imaginary wine merchant! This website uses lots of Javascript written by... guess who: you!

Here is one of the Web Cellar's forms: how many bottles do you want? In the source code, you'll see Javascript that:

Functions in Javascript are like functions (subroutines, procedures, code modules, subprograms) in other languages.

In the example in Chapter 1, the Javascript code was located inside an HTML tag. However, when there's more than a line or two of code, it's convenient to locate the code elsewhere in the file. The <head> of the document is a good place.

Starting with HTML 3.2, the <script> tag is available to embed Javascript and other languages in web pages.

To avoid confusing older, non-Javascript-equipped browsers, the code should be embedded in a comment. The comment tag markers ( <-- and --> ) must themselves be commented to avoid confusing the Javascript interpreter.

Javascript has many of the features found in other programming languages, including:
  • functions
  • parameters passed to functions
  • if-then-else statements
  • the + string concatenation operator
  • comparison operators such as <= and >=
  • logical operators such as && and ||
  • braces ( { } ) for enclosing code blocks
  • objects (T1 in this example) and their properties (value in this example)


Lab

Your task:

Solution

Don't peek unless you're stuck!