Counter Example "Answer"

<SCRIPT>

/********** Cookie Functions **********/

function setCookie(name, value) {
     document.
cookie = name + "=" + escape(value);
}

function getCookie(name) {
     var dc = document.
cookie;
     var prefix = name + "=";
     var begin = dc.
indexOf(prefix);

     if (begin == -1) return null;

     var end = document.cookie.indexOf(";", begin);

     if (end == -1) end = dc.length;

     return unescape(dc.substring(begin + prefix.length, end));
}

/********** End of Cookie Functions **********/

var counterStr = ""; // string variable that will hold the counter digits

var visits = getCookie("counter");

// if the counter cookie does not exist then make this the first visit
if (!visits) visits = 1;
// the "counter" is incremented here instead of incrementing at the end of the script
// NOTE: the counter cookie is a string so we need to convert to an integer
else visits =
parseInt(visits) + 1;

visits += ""; // converting the number to a string so that we can it's string properties.

/*
We have the advantage of knowing how the counter works, so we can use that to our advantage. When visits = 5 we know that the 3 numbers before 5 will be zeros and that the length of visits will be 1. When visits = 15 we know that the 2 numbers before 15 will be zeros and that the length of visits will be 2. So visits.charAt(0) for the example will be 5 while visits.charAt(1 - 3) don't exist, so we make them 0s.
*/
for (i = 0; i < 4; i++) {
     if (!visits.charAt(i)) counterStr = '<IMG SRC="odometer/0.gif">' + counterStr;
     else counterStr += '<IMG SRC="odometer/' + visits.charAt(i) + '.gif">';
}

setCookie("counter", visits);

</SCRIPT>

<BODY>

etc...

<SCRIPT>

document.write(counterStr); // write out the counter here

</SCRIPT>

</BODY>