NO STARCH PRESS
"the finest in geek entertainment"™
Home  | Catalog  | Where to buy  | About  | Jobs  | Media  | Blog  | Cart


book main | contents | reviews | updates

In the answer to the Chapter 7 assignment, the updateClock() function (pg. 365) uses loops, a topic not covered until Chapter 8. Here is the function as it appears on page 365:

function updateClock()
{
    var selected_zone = "";
    for (var loop=0; loop < window.document.clock_form.zones.length; loop++)
    {
        if (window.document.clock_form.zones[loop].checked == true)
        {
             selected_zone = window.document.clock_form.zones[loop].value;
        }
    }
    updateReadout(selected_zone);
}

Although not as elegant, there is a solution that doesn't use loops. You can achieve the same functionality as above with a bunch of else-if statements (see pages 40-41 for a discussion of if-then-else-if statements):

function updateClock()
{
    var selected_zone = "";

    if (window.document.clock_form.zones[0].checked == true)
    {
         selected_zone = window.document.clock_form.zones [0].value;
    }
    else if (window.document.clock_form.zones[1].checked == true)
    {
         selected_zone = window.document.clock_form.zones [1].value;
    }
    else if (window.document.clock_form.zones[2].checked == true)
    {
         selected_zone = window.document.clock_form.zones [2].value;
    }
    else if (window.document.clock_form.zones[3].checked == true)
    {
         selected_zone = window.document.clock_form.zones [3].value;
    }

    updateReadout(selected_zone);

}

book main | contents | reviews | updates



Copyright 2008 by No Starch Press