Labs

Lab 5.5: Functions, Revisited

We have already made extensive use of JavaScript functions in our order page. We have invoked some of JavaScript's builtin functions and have written a few of our own. In this set of exercises we'll write some new ones from scratch to further enhance our pages.

  1. In the Lab 5.4 exercises, we had you edit your page to make sure that each of the "Qty" entries was legitimate before attempting any processing. Since there are six "Qty" fields on our page, the same conditional processing had to be applied to each of the fields. This resulted in lots of duplicate (or, at least, nearly duplicate) code in our script. JavaScript functions are made, in part, to help us to eliminate such duplication of code.
    1. Write a function with header:
      function valid(thisQty)
      Function valid should return a value of true or false, indicating whether or not the numeric quantity provided (thisQty) is valid in the sense that we have defined validity for quantities on our order form.
    2. Now, edit function "submitForm" so that it eliminates the duplicate code used previously, and uses function valid instead to test each of the quantity fields. If any one of them is not valid, an alert box should be presented with an informative message, and function "submitForm" should be exited.


  2. Using this same basic approach, you can now write a function with header:
    function checkZip(zipString)
    to determine whether or not the string provided (zipString) constitutes a legitimate zip code. This time, we'll leave it up to you to come up with your own interpretation of what constitutes a legal zip code. As before, if the zip code entered does not conform to your definition of legal, an alert box with an appropriate message should be displayed, and processing should be discontinued.

Labs

MODULES:



© 2004 Thomson/Brooks Cole, All Rights Reserved.