Most of the JavaScript scripts that
you have seen so far have been simple sequences of statements. As we
attempt to write more complex functions, we find we need to be able to express
complicated orderings. Toward this end JavaScript provides a few standard
“control structures” that allow our functions to perform logical selection (if some
condition is true, then do these statements; if that same condition is false, then
do these other statements) and iteration (do this group of statements over and
over again, for some specified number of times). The exercises below focus on
JavaScript’s “if” and “for” statements.
-
Review the scripts of your Finder page to see how the “Find Island” button
accomplishes its processing.
-
Use this version of the HTML/JavaScript
interpreter to complete the following exercises:
- Write a statement that will present an alert box announcing “ZERO!!!” if the
value of text element “Number 1” is zero.
- Write a statement that will present an alert box announcing “ZERO!!!” if the
value of text element “Number 1” is zero, and will present an alert box
announcing “NOT ZERO!!!” if it isn’t.
- Write a statement that will display the string “Number 1 is bigger” in the text
element Result if the value of element Number 1 is greater than the value of
element Number 2; otherwise it should display the message “Number 2 is bigger"
in element Result.
- Write a statement that displays the message "Both Positive!" in the Result text element if the values of elements Number 1 and Number 2 are both greater than zero.
- Write JavaScript statements that display the sum of the integers 1-1000 in
the Result text element.
- Write JavaScript statements that display the average of the integers 1-1000
in the Result text element.
- Write the JavaScript statements needed to present a prompt box (displaying
the message “Which number into Result?” and two buttons, one for “Number 1”
and one for “Number 2”.) If the user clicks on button “Number 1”, the value of
text element “Number 1” should be copied in the Result element. If the user
clicks on button “Number 2”, the value of text element “Number 2” should be
copied into Result.
.
-
Now, return to your CVP pages and make the following extensions.
- Change the Summary page so that instead of presenting an alert box when
the user attempts to type into one of the text elements, it presents a prompt box
asking for a new value. If the user then responds by clicking “OK”, the new
value is placed in the appropriate text element. If the user clicks “Cancel”, no
change to the text box is made.
- Change the Finder page so that when you click on the "Find Island" button, and there is no island in the price range specified, a message appears in the text area indicating what has happened.
- Add a function to the Summary page that finds and displays the islands with the best beach values, and invoke the function using the "Best Beaches" button (that you added to the page in exercise 5.2.5f).
|