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:
- defines a function
- tests the entered value
- pops up alert dialogs
- concatenates strings
- uses a property of an object
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:
- Make a copy of the example form and edit it as follows.
- If 12 or more bottles are ordered, notify the user
that they will receive a 5% discount
- If 24 or more bottles are ordered, notify the user
that they will receive a 10% discount
- Open your modified form in your browser and test it.
Solution
Don't peek unless you're stuck!