<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>KirkHings.com &#187; self-instructional challenge</title>
	<atom:link href="http://kirkhings.com/tag/self-instructional-challenge/feed/" rel="self" type="application/rss+xml" />
	<link>http://kirkhings.com</link>
	<description>The '-berger' part trips up so many folks that I sometimes simplify my name for you. Yes, just for you.</description>
	<lastBuildDate>Sat, 13 Jun 2009 18:35:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Phrase-O-Matic Consultant Wisdom via Java</title>
		<link>http://kirkhings.com/code/java/phrase-o-matic-consultant-wisdom-via-java/419/</link>
		<comments>http://kirkhings.com/code/java/phrase-o-matic-consultant-wisdom-via-java/419/#comments</comments>
		<pubDate>Sat, 13 Jun 2009 18:35:59 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[head first java]]></category>
		<category><![CDATA[randomizer]]></category>
		<category><![CDATA[self-instructional challenge]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=419</guid>
		<description><![CDATA[ photo credit: schedetzki
Challenge: Create three lists of buzzwords typically used by business consultants. Randomly select three words and put in a sentence to form something ridiculous that a business consultant might say with an air of authority.

My solution:

package phraseomatic;

/**
 *
 * @author kirkster
 */
public class Main {

    /**
    [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/java/bottles-of-beer-song-via-java/388/' rel='bookmark' title='Permanent Link: Bottles of Beer Song via Java'>Bottles of Beer Song via Java</a> <small> photo credit: NUCO This programming challenge is from the...</small></li>
<li><a href='http://kirkhings.com/code/java/plain-and-recursive-factorials-with-full-print-enhancements/239/' rel='bookmark' title='Permanent Link: Plain and Recursive Factorials with Full Print Enhancements'>Plain and Recursive Factorials with Full Print Enhancements</a> <small> photo credit: gadl This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/random-bingo-card-generator-with-ajax-reset/224/' rel='bookmark' title='Permanent Link: Random BINGO Card Generator with Ajax Reset'>Random BINGO Card Generator with Ajax Reset</a> <small> photo credit: Keees This programming challenge is from a...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/28590840@N08/2660235237/" title="Peter Schedetzki" target="_blank"><img src="http://farm4.static.flickr.com/3065/2660235237_2c202c38a3.jpg" alt="Peter Schedetzki" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by-nd/2.0/" title="Attribution-NoDerivs License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/28590840@N08/2660235237/" title="schedetzki" target="_blank">schedetzki</a></small></div>
<blockquote><p>Challenge: Create three lists of buzzwords typically used by business consultants. Randomly select three words and put in a sentence to form something ridiculous that a business consultant might say with an air of authority.
</p></blockquote>
<p>My solution:</p>
<pre class="brush: java;">
package phraseomatic;

/**
 *
 * @author kirkster
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // create three word sets to build from
        String[] wordListOne = {&quot;24/7&quot;, &quot;multi-Tier&quot;, &quot;30,000 foot&quot;, &quot;B2B&quot;, &quot;win-win&quot;, &quot;front-end&quot;, &quot;web-based&quot;, &quot;pervasive&quot;, &quot;smart&quot;, &quot;six-sigma&quot;, &quot;critical-path&quot;, &quot;dynamic&quot;};
        String[] wordListTwo = {&quot;empowered&quot;, &quot;sticky&quot;, &quot;value-added&quot;, &quot;oriented&quot;, &quot;centric&quot;, &quot;distributed&quot;, &quot;clustered&quot;, &quot;branded&quot;, &quot;outside-the-box&quot;, &quot;positioned&quot;, &quot;networked&quot;, &quot;focused&quot;, &quot;leveraged&quot;, &quot;aligned&quot;, &quot;targeted&quot;, &quot;shared&quot;, &quot;cooperative&quot;, &quot;accelerated&quot;, &quot;semantic&quot;};
        String[] wordListThree = {&quot;process&quot;, &quot;tipping-point&quot;, &quot;solution&quot;, &quot;architecture&quot;, &quot;core competency&quot;, &quot;strategy&quot;, &quot;mindshare&quot;, &quot;portal&quot;, &quot;space&quot;, &quot;vision&quot;, &quot;paradigm&quot;, &quot;mission&quot;};

        // determine how many words in each list
        int oneLength = wordListOne.length;
        int twoLength = wordListTwo.length;
        int threeLength = wordListThree.length;

        // generate three random numbers
        int rand1 = (int) (Math.random() * oneLength);
        int rand2 = (int) (Math.random() * twoLength);
        int rand3 = (int) (Math.random() * threeLength);

        // build a phrase
        String phrase = wordListOne[rand1] + &quot; &quot; + wordListTwo[rand2] + &quot; &quot; + wordListThree[rand3];

        // print out the phrase
        System.out.println(&quot;Consultant says: \&quot;We need a &quot; + phrase + &quot;!\&quot;&quot;);
    }

}
</pre>
<p>Here is the output:</p>
<div id="attachment_420" class="wp-caption alignleft" style="width: 410px"><img src="http://kirkhings.com/wp-content/uploads/phraseomatic.png" alt="Phrase-O-Matic pumps out business consultant wisdom" title="phraseomatic" width="400" height="414" class="size-full wp-image-420" /><p class="wp-caption-text">Phrase-O-Matic pumps out business consultant wisdom</p></div>
<p>This programming challenge is from the Head First Java book, which I studied and practiced spring 2009. The end result itself is not terribly sexy, but I liked the algorithm construction process. My programming curriculum changed course during the first year, and they eliminated our Java classes. I was still curious about Java, so I took it upon myself to self-learn Java. This was one of my first products.</p>
<p>I post these self-instructional challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it. Finally, self-instructional challenges are interesting and different than academic challenges because I sought, discovered, and performed them on my own volition without anyone else telling me to do so. I did it because I wanted to learn and practice.</p>


<p>Related posts:<ul><li><a href='http://kirkhings.com/code/java/bottles-of-beer-song-via-java/388/' rel='bookmark' title='Permanent Link: Bottles of Beer Song via Java'>Bottles of Beer Song via Java</a> <small> photo credit: NUCO This programming challenge is from the...</small></li>
<li><a href='http://kirkhings.com/code/java/plain-and-recursive-factorials-with-full-print-enhancements/239/' rel='bookmark' title='Permanent Link: Plain and Recursive Factorials with Full Print Enhancements'>Plain and Recursive Factorials with Full Print Enhancements</a> <small> photo credit: gadl This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/random-bingo-card-generator-with-ajax-reset/224/' rel='bookmark' title='Permanent Link: Random BINGO Card Generator with Ajax Reset'>Random BINGO Card Generator with Ajax Reset</a> <small> photo credit: Keees This programming challenge is from a...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/java/phrase-o-matic-consultant-wisdom-via-java/419/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Random Image Display with JavaScript</title>
		<link>http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/</link>
		<comments>http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/#comments</comments>
		<pubDate>Mon, 25 May 2009 03:27:24 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[lynda]]></category>
		<category><![CDATA[self-instructional challenge]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=406</guid>
		<description><![CDATA[ photo credit: davidChief
This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/random-bingo-card-generator-with-ajax-reset/224/' rel='bookmark' title='Permanent Link: Random BINGO Card Generator with Ajax Reset'>Random BINGO Card Generator with Ajax Reset</a> <small> photo credit: Keees This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/slideshow-from-images-array/380/' rel='bookmark' title='Permanent Link: Slideshow from Images Array'>Slideshow from Images Array</a> <small> photo credit: pawpaw67 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/35395389@N00/516645478/" title="Awkward glance" target="_blank"><img src="http://farm1.static.flickr.com/232/516645478_0f73bb7bc8.jpg" alt="Awkward glance" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by/2.0/" title="Attribution License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/35395389@N00/516645478/" title="davidChief" target="_blank">davidChief</a></small></div>
<p>This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result itself is not terribly sexy, but I liked the algorithm construction process.</p>
<p>I post these self-instructional challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it. Finally, self-instructional challenges are interesting and different than academic challenges because I sought, discovered, and performed them on my own volition without anyone else telling me to do so. I did it because I wanted to learn and practice.</p>
<blockquote><p>Challenge: Create a simple script to randomly pull and display an image on a web page.
</p></blockquote>
<p>The HTML solution:</p>
<pre class="brush: xml;">
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;NASA Astronomy Picture of the Day Gallery&lt;/title&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;script.js&quot;&gt;&lt;/script&gt;
	&lt;link rel=&quot;stylesheet&quot; rev=&quot;stylesheet&quot; href=&quot;styles.css&quot; /&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;img src=&quot;images/spacer.gif&quot; id=&quot;aRandomPicture&quot; alt=&quot;a Random image&quot; /&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>The JavaScript solution:</p>
<pre class="brush: jscript;">
// run randomPic function whenever page loads
window.onload = randomPic;

// setup array of image files
var myPics = new Array(&quot;images/1.jpg&quot;, &quot;images/2.jpg&quot;, &quot;images/3.jpg&quot;, &quot;images/4.jpg&quot;, &quot;images/5.jpg&quot;, &quot;images/6.jpg&quot;, &quot;images/7.jpg&quot;, &quot;images8.jpg&quot;);

function randomPic() {
	// generate random number based on image quantity
	randomNumber = Math.floor((Math.random() * myPics.length));
	// set the image src attribute to a random pic in the image array
	document.getElementById(&quot;aRandomPicture&quot;).src = myPics[randomNumber];
}
</pre>
<p>It&#8217;s as simple as that!</p>


<p>Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/random-bingo-card-generator-with-ajax-reset/224/' rel='bookmark' title='Permanent Link: Random BINGO Card Generator with Ajax Reset'>Random BINGO Card Generator with Ajax Reset</a> <small> photo credit: Keees This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/slideshow-from-images-array/380/' rel='bookmark' title='Permanent Link: Slideshow from Images Array'>Slideshow from Images Array</a> <small> photo credit: pawpaw67 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Bottles of Beer Song via Java</title>
		<link>http://kirkhings.com/code/java/bottles-of-beer-song-via-java/388/</link>
		<comments>http://kirkhings.com/code/java/bottles-of-beer-song-via-java/388/#comments</comments>
		<pubDate>Sun, 24 May 2009 19:42:03 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[head first java]]></category>
		<category><![CDATA[self-instructional challenge]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=388</guid>
		<description><![CDATA[ photo credit: NUCO
This programming challenge is from the Head First Java book, which I studied and practiced spring 2009. The end result itself is not terribly sexy, but I liked the algorithm construction process. My programming curriculum changed course during the first year, and they eliminated our Java classes. I was still curious about [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/java/phrase-o-matic-consultant-wisdom-via-java/419/' rel='bookmark' title='Permanent Link: Phrase-O-Matic Consultant Wisdom via Java'>Phrase-O-Matic Consultant Wisdom via Java</a> <small> photo credit: schedetzki Challenge: Create three lists of buzzwords...</small></li>
<li><a href='http://kirkhings.com/code/java/plain-and-recursive-factorials-with-full-print-enhancements/239/' rel='bookmark' title='Permanent Link: Plain and Recursive Factorials with Full Print Enhancements'>Plain and Recursive Factorials with Full Print Enhancements</a> <small> photo credit: gadl This programming challenge is from a...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/89046340@N00/107340991/" title="PA300108" target="_blank"><img src="http://farm1.static.flickr.com/51/107340991_b1bda6f04d.jpg" alt="PA300108" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by/2.0/" title="Attribution License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/89046340@N00/107340991/" title="NUCO" target="_blank">NUCO</a></small></div>
<p>This programming challenge is from the Head First Java book, which I studied and practiced spring 2009. The end result itself is not terribly sexy, but I liked the algorithm construction process. My programming curriculum changed course during the first year, and they eliminated our Java classes. I was still curious about Java, so I took it upon myself to self-learn Java. This was one of my first products.</p>
<p>I post these self-instructional challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it. Finally, self-instructional challenges are interesting and different than academic challenges because I sought, discovered, and performed them on my own volition without anyone else telling me to do so. I did it because I wanted to learn and practice.</p>
<blockquote><p>Challenge: Create a simple program which outputs the classic &#8220;99 Bottles of Beer&#8221; song, counting down from 99 to 0 bottles. In addition to outputting the correct number of bottles in each chorus, output the correct plural or singular form of &#8220;bottle&#8221; where appropriate to the number remaining. When there are no more bottles left then state final line.
</p></blockquote>
<p>My solution:</p>
<pre class="brush: java;">
package bottlesofbeer;

/**
 *
 * @author kirk
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // initialize starting number of bottles on wall
        int beerNum = 99;

        // initialize plural form of bottle used most often
        String word = &quot;bottles&quot;;

        while (beerNum &gt; 0) {
            // if only 1 bottle is left then change to singular form
            if (beerNum == 1) { word = &quot;bottle&quot;; }

            // output song chorus using current variable states
            System.out.println(beerNum + &quot; &quot; + word + &quot; of beer on the wall,&quot;);
            System.out.println(beerNum + &quot; &quot; + word + &quot; of beer!&quot;);
            System.out.println(&quot;Take one down, pass it around.&quot;);

            // decrement beerNum cuz we took one down and passed it around
            beerNum--;
            if (beerNum == 1) { word = &quot;bottle&quot;; }

            // so long as there are still bottles on the wall
            if (beerNum &gt; 0) {
                System.out.println(beerNum + &quot; &quot; + word + &quot; of beer on the wall!&quot;);
                System.out.println();
            }
            // when there are no more bottles on the wall
            else {
                System.out.println(&quot;No more bottles of beer on the wall!&quot;);
            } // ends if/else
        } // ends while loop
    } // ends main method
} // ends class
</pre>
<p>Here is the output:</p>
<div id="attachment_394" class="wp-caption alignleft" style="width: 310px"><img src="http://kirkhings.com/wp-content/uploads/beersong-java.png" alt="99 Bottles of Beer Song output via Java" title="beersong-java" width="300" height="454" class="size-full wp-image-394" /><p class="wp-caption-text">99 Bottles of Beer Song output via Java</p></div>


<p>Related posts:<ul><li><a href='http://kirkhings.com/code/java/phrase-o-matic-consultant-wisdom-via-java/419/' rel='bookmark' title='Permanent Link: Phrase-O-Matic Consultant Wisdom via Java'>Phrase-O-Matic Consultant Wisdom via Java</a> <small> photo credit: schedetzki Challenge: Create three lists of buzzwords...</small></li>
<li><a href='http://kirkhings.com/code/java/plain-and-recursive-factorials-with-full-print-enhancements/239/' rel='bookmark' title='Permanent Link: Plain and Recursive Factorials with Full Print Enhancements'>Plain and Recursive Factorials with Full Print Enhancements</a> <small> photo credit: gadl This programming challenge is from a...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/java/bottles-of-beer-song-via-java/388/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Slideshow from Images Array</title>
		<link>http://kirkhings.com/code/javascript/slideshow-from-images-array/380/</link>
		<comments>http://kirkhings.com/code/javascript/slideshow-from-images-array/380/#comments</comments>
		<pubDate>Fri, 15 May 2009 19:57:23 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[lynda]]></category>
		<category><![CDATA[self-instructional challenge]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=380</guid>
		<description><![CDATA[ photo credit: pawpaw67
This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/' rel='bookmark' title='Permanent Link: Accessible JavaScript Rollover with Three-State Functionality'>Accessible JavaScript Rollover with Three-State Functionality</a> <small> photo credit: Rhian vK This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/7641646@N03/1302964167/" title="Thistle Seeds" target="_blank"><img src="http://farm2.static.flickr.com/1007/1302964167_a809f4945f.jpg" alt="Thistle Seeds" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by-sa/2.0/" title="Attribution-ShareAlike License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/7641646@N03/1302964167/" title="pawpaw67" target="_blank">pawpaw67</a></small></div>
<p>This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result itself is not terribly sexy, but I liked the algorithm construction process.</p>
<p>I post these self-instructional challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it. Finally, self-instructional challenges are interesting and different than academic challenges because I sought, discovered, and performed them on my own volition without anyone else telling me to do so. I did it because I wanted to learn and practice.</p>
<blockquote><p>Challenge: Create a JavaScript image slideshow with next and previous links that use an array. Loop the script so the slideshow never reaches an end.
</p></blockquote>
<p>The HTML solution:</p>
<pre class="brush: xml;">
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;NASA Patches Slideshow&lt;/title&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;script.js&quot;&gt;&lt;/script&gt;
	&lt;link rel=&quot;stylesheet&quot; rev=&quot;stylesheet&quot; href=&quot;styles.css&quot; /&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;div align=&quot;center&quot;&gt;
		&lt;h1&gt;NASA Patches Slideshow&lt;/h1&gt;
		&lt;img src=&quot;images/nasa.jpeg&quot; id=&quot;currentPicture&quot; width=&quot;201&quot; height=&quot;155&quot; alt=&quot;Slideshow&quot; /&gt;
		&lt;h2&gt;
        	&lt;a href=&quot;previous.html&quot; id=&quot;prevLink&quot;&gt;&amp;laquo; Previous&lt;/a&gt;&amp;nbsp;&amp;nbsp;
            &lt;a href=&quot;next.html&quot; id=&quot;nextLink&quot;&gt;Next &amp;raquo;&lt;/a&gt;&lt;/h2&gt;
	&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>The JavaScript solution:</p>
<pre class="brush: jscript;">
window.onload = initLinks;

// create array of pictures/strings
var myPics = new Array(&quot;images/nasa.jpeg&quot;, &quot;images/columbia1.gif&quot;, &quot;images/columbia2.jpeg&quot;, &quot;images/columbia3.jpg&quot;, &quot;images/columbia4.jpeg&quot;, &quot;images/challenger.jpeg&quot;);

// track currently displayed image, set to 0
var thisPic = 0;

// handle behaviors when next and previous links are clicked
function initLinks() {
	// when prevLink is clicked, call function to fetch the previous pic
	document.getElementById(&quot;prevLink&quot;).onclick = processPrevious;
	// when nextLink is clicked, call function to fetch the next pic
	document.getElementById(&quot;nextLink&quot;).onclick = processNext;
}

// called when prevLink is clicked
function processPrevious() {
	// if we are on slide 0
	if (thisPic == 0) {
		// then set thisPic to be same value as array length (meaning last picture)
		// this allows looping around the collection not reaching an end
		thisPic = myPics.length;
	}
	// decrement thisPic to be one less than last pic so as to point to previous
	thisPic--;
	// set source of page's displayed picture to be new current
	document.getElementById(&quot;currentPicture&quot;).src = myPics[thisPic];
	// js handled the work, so prevent next page from being fetched
	return false;
}

// called when nextLink is clicked
function processNext() {
	// incerement thisPic to be one more than last pic so as to point to next
	thisPic++;
	// if we are on last picture in array
	if (thisPic == myPics.length) {
		// then set thisPic to be original value in array (meaning first picture)
		// this allows looping around the collection not reaching an end
		thisPic = 0;
	}
	// set source of page's displayed picture to be new current
	document.getElementById(&quot;currentPicture&quot;).src = myPics[thisPic];
	// js handled the work, so prevent next page from being fetched
	return false;
}
</pre>
<p>Here is a screenshot example of a previous slideshow image:<br />
<div id="attachment_382" class="wp-caption alignleft" style="width: 466px"><img src="http://kirkhings.com/wp-content/uploads/js-nasapatches2.png" alt="Example of the previous image fetched" title="js-nasapatches2" width="456" height="306" class="size-full wp-image-382" /><p class="wp-caption-text">Example of the previous image fetched</p></div></p>
<p>Here is a screenshot example of a next slideshow image:<br />
<div id="attachment_381" class="wp-caption alignleft" style="width: 466px"><img src="http://kirkhings.com/wp-content/uploads/js-nasapatches1.png" alt="Example of the next image fetched" title="js-nasapatches1" width="456" height="306" class="size-full wp-image-381" /><p class="wp-caption-text">Example of the next image fetched</p></div></p>


<p>Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/' rel='bookmark' title='Permanent Link: Accessible JavaScript Rollover with Three-State Functionality'>Accessible JavaScript Rollover with Three-State Functionality</a> <small> photo credit: Rhian vK This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/javascript/slideshow-from-images-array/380/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Disjointed JavaScript Rollover Causes Separate Animation</title>
		<link>http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/</link>
		<comments>http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 19:57:17 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[lynda]]></category>
		<category><![CDATA[self-instructional challenge]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=324</guid>
		<description><![CDATA[ photo credit: Matt McGee
This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/' rel='bookmark' title='Permanent Link: Accessible JavaScript Rollover with Three-State Functionality'>Accessible JavaScript Rollover with Three-State Functionality</a> <small> photo credit: Rhian vK This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/51035750608@N01/183123043/" title="July 5, 2006: Catawampus" target="_blank"><img src="http://farm1.static.flickr.com/70/183123043_35ef17f96c.jpg" alt="July 5, 2006: Catawampus" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by-nd/2.0/" title="Attribution-NoDerivs License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/51035750608@N01/183123043/" title="Matt McGee" target="_blank">Matt McGee</a></small></div>
<p>This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result itself is not terribly sexy, but I liked the algorithm construction process.</p>
<p>I post these self-instructional challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it. Finally, self-instructional challenges are interesting and different than academic challenges because I sought, discovered, and performed them on my own volition without anyone else telling me to do so. I did it because I wanted to learn and practice.</p>
<blockquote><p>Challenge: Create a JavaScript image rollover animation which includes a default image, and a hover over image. Use abstracted functions. Improve accessibility by creating onfocus and onblur event handlers for users without mice. Make the rollover occur when you mouse over a different document element.
</p></blockquote>
<p>The HTML solution:</p>
<pre class="brush: xml;">
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;A Disjointed Link Rollover&lt;/title&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;script.js&quot;&gt;&lt;/script&gt;
	&lt;link rel=&quot;stylesheet&quot; rev=&quot;stylesheet&quot; href=&quot;styles.css&quot; /&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;h1&gt;
		&lt;a href=&quot;next.html&quot; id=&quot;arrow&quot;&gt;Next page&lt;/a&gt;
	&lt;/h1&gt;
	&lt;img src=&quot;images/arrow_off.gif&quot; width=&quot;147&quot; height=&quot;82&quot; id=&quot;arrow_image&quot; alt=&quot;arrow&quot; /&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>The CSS solution:</p>
<pre class="brush: css;">
body {
	background-color: white;
}
img {
	border-width: 0;
}
</pre>
<p>The JavaScript solution:</p>
<pre class="brush: jscript;">
// initialize the rollovers when window loads
window.onload = rolloverInit;

function rolloverInit() {
	// loop through all links on page
	for (var i=0; i&lt;document.links.length; i++) {
		// capture i-th link as local variable to make life easier later on
		var linkObject = document.links[i];
		// if current linkObject has an id (meaning it is something we care to change)
		if (linkObject.id) {
			// append current image's id with _image (preplanned format), save to local variable
			var imageObject = document.getElementById(linkObject.id + &quot;_image&quot;);
			// if the imageObject exists
			if (imageObject) {
				// pass both current local variables to setupRollover function
				setupRollover(linkObject, imageObject);
			}
		}
	}
}

// 'thisLink &amp; thisImage' passed from rolloverInit, meaning we need to setup the rollover for this particular link &amp; image
function setupRollover(thisLink, thisImage) {
	// capture the current link's associated image as thisLink's intended target image property
	thisLink.imgToChange = thisImage;
	// capture rollOut function as behavior when thisLinks's onmouseout event handler is triggered
	thisLink.onmouseout = rollOut;
	// capture rollOver function as behavior when thisImage's onmouseover event handler is triggered
	thisLink.onmouseover = rollOver;

	// create new child image object for thisLink's  outImage property
	thisLink.outImage = new Image();
	// store current image src as the source for overImage, so it stays same when not rolled over
	thisLink.outImage.src = thisImage.src;

	// create new child image object for thisLink's  overImage property
	thisLink.overImage = new Image();
	// store current image src as the source for overImage, so it stays same when not rolled over
	thisLink.overImage.src = &quot;images/&quot; + thisLink.id + &quot;_on.gif&quot;;

	// improve accessibility
	// simply set thisLink's onblur and onfocus event handlers to rollOut and rollOver respectively
	thisLink.onblur = rollOut;
	thisLink.onfocus = rollOver;
}	

function rollOut() {
	// change thisLinks's image src to the outImage source property (already captured)
	this.imgToChange.src = this.outImage.src;
}

function rollOver() {
	// change thisLinks's image src to the overImage source property (already captured)
	this.imgToChange.src = this.overImage.src;
}
</pre>
<p>And finally here is a screenshot of the link highlighted result:<br />
<img src="http://kirkhings.com/wp-content/uploads/linkrollover.png" alt="linkrollover" title="linkrollover" width="398" height="181" class="alignleft size-full wp-image-325" /></p>


<p>Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/' rel='bookmark' title='Permanent Link: Accessible JavaScript Rollover with Three-State Functionality'>Accessible JavaScript Rollover with Three-State Functionality</a> <small> photo credit: Rhian vK This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Accessible JavaScript Rollover with Three-State Functionality</title>
		<link>http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/</link>
		<comments>http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 18:55:08 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[accessibility]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[lynda]]></category>
		<category><![CDATA[self-instructional challenge]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=319</guid>
		<description><![CDATA[ photo credit: Rhian vK
This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/' rel='bookmark' title='Permanent Link: Disjointed JavaScript Rollover Causes Separate Animation'>Disjointed JavaScript Rollover Causes Separate Animation</a> <small> photo credit: Matt McGee This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/34764035@N00/2258204466/" title="PRIORITE" target="_blank"><img src="http://farm3.static.flickr.com/2391/2258204466_3162c3f37f.jpg" alt="PRIORITE" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by/2.0/" title="Attribution License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/34764035@N00/2258204466/" title="Rhian vK" target="_blank">Rhian vK</a></small></div>
<p>This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result itself is not terribly sexy, but I liked the algorithm construction process.</p>
<p>I post these self-instructional challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it. Finally, self-instructional challenges are interesting and different than academic challenges because I sought, discovered, and performed them on my own volition without anyone else telling me to do so. I did it because I wanted to learn and practice.</p>
<blockquote><p>Challenge: Create a JavaScript image rollover animation which includes a default image, a hover over image, and an image when the link is clicked. Use abstracted functions. Improve accessibility by creating onfocus and onblur event handlers for users without mice.
</p></blockquote>
<p>The HTML solution:</p>
<pre class="brush: xml;">
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;An Accessible Three-State Rollover&lt;/title&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;script.js&quot;&gt;&lt;/script&gt;
	&lt;link rel=&quot;stylesheet&quot; rev=&quot;stylesheet&quot; href=&quot;styles.css&quot; /&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;a href=&quot;next1.html&quot;&gt;
		&lt;img src=&quot;images/button1_off.gif&quot; width=&quot;113&quot; height=&quot;33&quot; alt=&quot;button1&quot; id=&quot;button1&quot; /&gt;
	&lt;/a&gt;&amp;nbsp;&amp;nbsp;
	&lt;a href=&quot;next2.html&quot;&gt;
		&lt;img src=&quot;images/button2_off.gif&quot; width=&quot;113&quot; height=&quot;33&quot; alt=&quot;button2&quot; id=&quot;button2&quot; /&gt;
		&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>The CSS solution:</p>
<pre class="brush: css;">
body {
	background-color: white;
}
img {
	border-width: 0;
}
</pre>
<p>The JavaScript solution:</p>
<pre class="brush: jscript;">
// initialize the rollovers when window loads
window.onload = rolloverInit;

function rolloverInit() {
	// loop through all images on page
	for (var i = 0; i &lt; document.images.length; i++) {
		// if current image is wrapped in an anchor tag (i.e. &lt;a href=&quot;&quot;&gt;&lt;img&gt;&lt;/a&gt;)
		if (document.images[i].parentNode.tagName == &quot;A&quot;) {
			// initiate rollover setup for this current image, pass the current image to that function
			setupRollover(document.images[i]);
		}
	}
}

// 'thisImage' passed from rolloverInit, meaning we need to setup the rollover for this particular image
function setupRollover(thisImage) {
	// create new child image object for thisImage object's outImage property
	thisImage.outImage = new Image();
	// store current image src as the source for outimage, so it stays same when not rolled over
	thisImage.outImage.src = thisImage.src;
	// capture rollOut function as behavior when thisImage's onmouseout event handler is triggered
	thisImage.onmouseout = rollOut;

	// create new child image object for thisImage object's overImage property
	thisImage.overImage = new Image();
	// store preconsructed '_on'-state image src (using thisImage's captured id/name) as the source for overimage, so it stays changes when rolled over
	thisImage.overImage.src = &quot;images/&quot; + thisImage.id + &quot;_on.gif&quot;;
	// capture rollOver function as behavior when thisImage's onmouseover event handler is triggered
	thisImage.onmouseover = rollOver;

	// create new child image object for thisImage object's clickImage property
	thisImage.clickImage = new Image();
	// store preconsructed '_on'-state image src (using thisImage's captured id/name) as the source for overimage, so it stays changes when rolled over
	thisImage.clickImage.src = &quot;images/&quot; + thisImage.id + &quot;_click.gif&quot;;
	// capture rollClick function as behavior when thisImage's onmousedown event handler is triggered
	thisImage.onmousedown = rollClick;

	// improve accessibility
	// set image's parent anchor child Image to thisImage
	thisImage.parentNode.childImage = thisImage;
	// when image's achor tag is unselected (an event handler)
	thisImage.parentNode.onblur = rollOutChild;
	// when image's achor tag is selected (an event handler)
	thisImage.parentNode.onfocus = rollOverChild;
}

function rollOut() {
	// change thisImage's src to the outImage source property (already captured)
	this.src = this.outImage.src;
}

function rollOver() {
	// change thisImage's src to the overImage source property (already captured)
	this.src = this.overImage.src;
}

function rollClick() {
	// change thisImage's src to the clickImage source property (already captured)
	this.src = this.clickImage.src;
}

function rollOutChild() {
	// change the achor tag's childImage src to the childImage's outImage source property (already captured)
	this.childImage.src = this.childImage.outImage.src;
}

function rollOverChild() {
	// change the achor tag's childImage src to the childImage's overImage source property (already captured)
	this.childImage.src = this.childImage.overImage.src;
}
</pre>
<p>And finally here is a screenshot of the improved end rollover result: </p>
<p>Rollover state A: <div id="attachment_301" class="wp-caption alignleft" style="width: 278px"><img src="http://kirkhings.com/wp-content/uploads/improvedrollovera.png" alt="Improved rollover state A" title="improvedrollovera" width="268" height="73" class="size-full wp-image-301" /><p class="wp-caption-text">Improved rollover state A</p></div></p>
<p>
Rollover state B: <div id="attachment_302" class="wp-caption alignleft" style="width: 263px"><img src="http://kirkhings.com/wp-content/uploads/improvedrolloverb.png" alt="Improved rollover state B" title="improvedrolloverb" width="253" height="51" class="size-full wp-image-302" /><p class="wp-caption-text">Improved rollover state B</p></div></p>
<p>
Rollover state C (clicked button): <div id="attachment_313" class="wp-caption alignleft" style="width: 264px"><img src="http://kirkhings.com/wp-content/uploads/improvedrolloverc.png" alt="Button state when button is clicked" title="improvedrolloverc" width="254" height="51" class="size-full wp-image-313" /><p class="wp-caption-text">Button state when button is clicked</p></div></p>
<div id="attachment_320" class="wp-caption alignleft" style="width: 408px"><img src="http://kirkhings.com/wp-content/uploads/accessiblerollover.png" alt="Demonstrating accessible rollover when button is selected but not moused over." title="accessiblerollover" width="398" height="58" class="size-full wp-image-320" /><p class="wp-caption-text">Demonstrating accessible rollover when button is selected but not moused over.</p></div>
<p></p>


<p>Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/' rel='bookmark' title='Permanent Link: Disjointed JavaScript Rollover Causes Separate Animation'>Disjointed JavaScript Rollover Causes Separate Animation</a> <small> photo credit: Matt McGee This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Three-State JavaScript Rollover</title>
		<link>http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/</link>
		<comments>http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 16:47:36 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[lynda]]></category>
		<category><![CDATA[self-instructional challenge]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=312</guid>
		<description><![CDATA[ photo credit: chimothy27
This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/' rel='bookmark' title='Permanent Link: Accessible JavaScript Rollover with Three-State Functionality'>Accessible JavaScript Rollover with Three-State Functionality</a> <small> photo credit: Rhian vK This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/' rel='bookmark' title='Permanent Link: Disjointed JavaScript Rollover Causes Separate Animation'>Disjointed JavaScript Rollover Causes Separate Animation</a> <small> photo credit: Matt McGee This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/8641421@N07/2787799499/" title="Will Rollover" target="_blank"><img src="http://farm4.static.flickr.com/3036/2787799499_6bc46bda11.jpg" alt="Will Rollover" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by-nd/2.0/" title="Attribution-NoDerivs License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/8641421@N07/2787799499/" title="chimothy27" target="_blank">chimothy27</a></small></div>
<p>This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result itself is not terribly sexy, but I liked the algorithm construction process.</p>
<p>I post these self-instructional challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it. Finally, self-instructional challenges are interesting and different than academic challenges because I sought, discovered, and performed them on my own volition without anyone else telling me to do so. I did it because I wanted to learn and practice.</p>
<blockquote><p>Challenge: Create a JavaScript image rollover animation which includes a default image, a hover over image, and an image when the link is clicked. Use abstracted functions.
</p></blockquote>
<p>The HTML solution:</p>
<pre class="brush: xml;">
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;A Three-State Rollover&lt;/title&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;script.js&quot;&gt;&lt;/script&gt;
	&lt;link rel=&quot;stylesheet&quot; rev=&quot;stylesheet&quot; href=&quot;styles.css&quot; /&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;a href=&quot;next1.html&quot;&gt;
		&lt;img src=&quot;images/button1_off.gif&quot; width=&quot;113&quot; height=&quot;33&quot; alt=&quot;button1&quot; id=&quot;button1&quot; /&gt;
	&lt;/a&gt;&amp;nbsp;&amp;nbsp;
	&lt;a href=&quot;next2.html&quot;&gt;
		&lt;img src=&quot;images/button2_off.gif&quot; width=&quot;113&quot; height=&quot;33&quot; alt=&quot;button2&quot; id=&quot;button2&quot; /&gt;
		&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>The CSS solution:</p>
<pre class="brush: css;">
body {
	background-color: white;
}
img {
	border-width: 0;
}
</pre>
<p>The JavaScript solution:</p>
<pre class="brush: jscript;">
// initialize the rollovers when window loads
window.onload = rolloverInit;

function rolloverInit() {
	// loop through all images on page
	for (var i = 0; i &lt; document.images.length; i++) {
		// if current image is wrapped in an anchor tag (i.e. &lt;a href=&quot;&quot;&gt;&lt;img&gt;&lt;/a&gt;)
		if (document.images[i].parentNode.tagName == &quot;A&quot;) {
			// initiate rollover setup for this current image, pass the current image to that function
			setupRollover(document.images[i]);
		}
	}
}

// 'thisImage' passed from rolloverInit, meaning we need to setup the rollover for this particular image
function setupRollover(thisImage) {
	// create new child image object for thisImage object's outImage property
	thisImage.outImage = new Image();
	// store current image src as the source for outimage, so it stays same when not rolled over
	thisImage.outImage.src = thisImage.src;
	// capture rollOut function as behavior when thisImage's onmouseout event handler is triggered
	thisImage.onmouseout = rollOut;

	// create new child image object for thisImage object's overImage property
	thisImage.overImage = new Image();
	// store preconsructed '_on'-state image src (using thisImage's captured id/name) as the source for overimage, so it stays changes when rolled over
	thisImage.overImage.src = &quot;images/&quot; + thisImage.id + &quot;_on.gif&quot;;
	// capture rollOver function as behavior when thisImage's onmouseover event handler is triggered
	thisImage.onmouseover = rollOver;

	// create new child image object for thisImage object's clickImage property
	thisImage.clickImage = new Image();
	// store preconsructed '_on'-state image src (using thisImage's captured id/name) as the source for overimage, so it stays changes when rolled over
	thisImage.clickImage.src = &quot;images/&quot; + thisImage.id + &quot;_click.gif&quot;;
	// capture rollClick function as behavior when thisImage's onmousedown event handler is triggered
	thisImage.onmousedown = rollClick;
}

function rollOut() {
	// change thisImage's src to the outImage source property (already captured)
	this.src = this.outImage.src;
}

function rollOver() {
	// change thisImage's src to the overImage source property (already captured)
	this.src = this.overImage.src;
}

function rollClick() {
	// change thisImage's src to the clickImage source property (already captured)
	this.src = this.clickImage.src;
}
</pre>
<p>And finally here is a screenshot of the improved end rollover result: </p>
<p>Rollover state A: <div id="attachment_301" class="wp-caption alignleft" style="width: 278px"><img src="http://kirkhings.com/wp-content/uploads/improvedrollovera.png" alt="Improved rollover state A" title="improvedrollovera" width="268" height="73" class="size-full wp-image-301" /><p class="wp-caption-text">Improved rollover state A</p></div></p>
<p>Rollover state B: <div id="attachment_302" class="wp-caption alignleft" style="width: 263px"><img src="http://kirkhings.com/wp-content/uploads/improvedrolloverb.png" alt="Improved rollover state B" title="improvedrolloverb" width="253" height="51" class="size-full wp-image-302" /><p class="wp-caption-text">Improved rollover state B</p></div></p>
<p>Rollover state C (clicked button): <div id="attachment_313" class="wp-caption alignleft" style="width: 264px"><img src="http://kirkhings.com/wp-content/uploads/improvedrolloverc.png" alt="Button state when button is clicked" title="improvedrolloverc" width="254" height="51" class="size-full wp-image-313" /><p class="wp-caption-text">Button state when button is clicked</p></div></p>


<p>Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/' rel='bookmark' title='Permanent Link: Accessible JavaScript Rollover with Three-State Functionality'>Accessible JavaScript Rollover with Three-State Functionality</a> <small> photo credit: Rhian vK This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/' rel='bookmark' title='Permanent Link: Disjointed JavaScript Rollover Causes Separate Animation'>Disjointed JavaScript Rollover Causes Separate Animation</a> <small> photo credit: Matt McGee This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/' rel='bookmark' title='Permanent Link: Simple and Improved Image Rollovers'>Simple and Improved Image Rollovers</a> <small> photo credit: Etwood This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery fadeOut upon click</title>
		<link>http://kirkhings.com/code/jquery/jquery-fadeout-upon-click/307/</link>
		<comments>http://kirkhings.com/code/jquery/jquery-fadeout-upon-click/307/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 19:50:52 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[self-instructional challenge]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=307</guid>
		<description><![CDATA[ photo credit: Joel Bedford
This programming snippet is from the themeforest.net video series &#8220;jQuery for beginners&#8221; which I watched and practiced spring 2009. I do not mean to steal from them, but just post how I performed their tutorials for my own later reference and perhaps save someone else the time of watching the videos.
I [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/jquery/disable-submit-button-to-prevent-double-submit/170/' rel='bookmark' title='Permanent Link: Disable Submit Button to Prevent Double-Submit'>Disable Submit Button to Prevent Double-Submit</a> <small> photo credit: kevindooley This programming snippet is from the...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/71078118@N00/2279307796/" title="self with colored tiles" target="_blank"><img src="http://farm3.static.flickr.com/2143/2279307796_c2b24cc4b3.jpg" alt="self with colored tiles" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by-nd/2.0/" title="Attribution-NoDerivs License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/71078118@N00/2279307796/" title="Joel Bedford" target="_blank">Joel Bedford</a></small></div>
<p>This programming snippet is from the themeforest.net video series &#8220;jQuery for beginners&#8221; which I watched and practiced spring 2009. I do not mean to steal from them, but just post how I performed their tutorials for my own later reference and perhaps save someone else the time of watching the videos.</p>
<p>I post these old code snippets for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it. Finally, it is just nice for me to have my own code snippet library, to easily find snippets that I remember doing a particular job but I cannot always recall where I found it.</p>
<blockquote><p>Challenge: Download jQuery script from jQuery.com (get the production version, not the development version). Link to the jQuery script file form your index.html. Write simple animation script to fade out a div element when a link is clicked.
</p></blockquote>
<p>Solution: </p>
<pre class="brush: xml;">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head&gt;
	&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;
	&lt;title&gt;Beginner's jQuery&lt;/title&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;jquery-1.3.2.min.js&quot;&gt;&lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;script.js&quot;&gt;&lt;/script&gt;
	&lt;style type=&quot;text/css&quot;&gt;
		#box {
			background: blue;
			width: 200px;
			height: 100px;
		}
	&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;div id=&quot;box&quot;&gt;&lt;/div&gt;
	&lt;a href=&quot;#&quot;&gt;Click me&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Here is the script to animate the box fading away: </p>
<pre class="brush: jscript;">
// start a generic function
$(function() {
	// when link is clicked, start a sub-function
	$('a').click(function() {
		// make the element with id='box' fade out over 5 seconds
		$('#box').fadeOut(5000);
	});
});
</pre>
<p>Note that you can also use fadeOut(&#8217;slow&#8217;); or &#8216;fast&#8217; without specifying actual milliseconds.</p>


<p>Related posts:<ul><li><a href='http://kirkhings.com/code/jquery/disable-submit-button-to-prevent-double-submit/170/' rel='bookmark' title='Permanent Link: Disable Submit Button to Prevent Double-Submit'>Disable Submit Button to Prevent Double-Submit</a> <small> photo credit: kevindooley This programming snippet is from the...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/jquery/jquery-fadeout-upon-click/307/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple and Improved Image Rollovers</title>
		<link>http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/</link>
		<comments>http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 17:24:14 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[lynda]]></category>
		<category><![CDATA[self-instructional challenge]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=298</guid>
		<description><![CDATA[ photo credit: Etwood
This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/' rel='bookmark' title='Permanent Link: Accessible JavaScript Rollover with Three-State Functionality'>Accessible JavaScript Rollover with Three-State Functionality</a> <small> photo credit: Rhian vK This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/' rel='bookmark' title='Permanent Link: Disjointed JavaScript Rollover Causes Separate Animation'>Disjointed JavaScript Rollover Causes Separate Animation</a> <small> photo credit: Matt McGee This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/44788018@N00/2874889018/" title="Ziggy" target="_blank"><img src="http://farm4.static.flickr.com/3278/2874889018_5846e2b179.jpg" alt="Ziggy" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by/2.0/" title="Attribution License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/44788018@N00/2874889018/" title="Etwood" target="_blank">Etwood</a></small></div>
<p>This programming challenge is from a Lynda.com instructional video, featuring Dori Smith, which I studied/practiced last summer but once again spring 2009. I do not mean to just re-post the wonderful Lynda.com code, but rather add instructional value to it by way of clearer variable names and explanatory comments. The end result itself is not terribly sexy, but I liked the algorithm construction process.</p>
<p>I post these self-instructional challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it. Finally, self-instructional challenges are interesting and different than academic challenges because I sought, discovered, and performed them on my own volition without anyone else telling me to do so. I did it because I wanted to learn and practice.</p>
<blockquote><p>Challenge: Create a simple javascript image rollover animation. (Two arrow images colored red and blue provided in my set, but you can use any two images you like.) Do basic version first, then improve it by separating script from display code, and abstract the functions.
</p></blockquote>
<p><strong>The Simple Method: </strong></p>
<p>The HTML solution:</p>
<pre class="brush: xml;">
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;A Simple Rollover&lt;/title&gt;
	&lt;link rel=&quot;stylesheet&quot; rev=&quot;stylesheet&quot; href=&quot;styles.css&quot; /&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;a href=&quot;next.html&quot; onmouseover=&quot;document.arrow.src='images/arrow_on.gif'&quot; onmouseout=&quot;document.arrow.src='images/arrow_off.gif'&quot;&gt;&lt;img src=&quot;images/arrow_off.gif&quot; width=&quot;147&quot; height=&quot;82&quot; alt=&quot;arrow&quot; name=&quot;arrow&quot; /&gt;&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>The CSS solution:</p>
<pre class="brush: css;">
body {
	background-color: white;
}
img {
	border-width: 0;
}
</pre>
<p>This is a quick and dirty method, but it has problems. The biggest problem is the rollover does not execute until the user moves their mouse over the image. If the new image is large or the user&#8217;s connection is slow, there will be a noticeable delay in the first time rollover.</p>
<p>Another problem is that are now committed to updating JavaScript code within HTML code. This causes many efficiency hassles, such as intermingling design work with scripting work, and violating Model-View-Controller (MVC) principles.</p>
<p>A better method is to separate the JavaScript from the HTML page. This next improved method will attempt Don&#8217;t Repeat Yourself (DRY) principles.</p>
<p><strong>The Improved Method: </strong></p>
<p>The HTML solution:</p>
<pre class="brush: xml;">
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;An Improved Rollover&lt;/title&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;script.js&quot;&gt;&lt;/script&gt;
	&lt;link rel=&quot;stylesheet&quot; rev=&quot;stylesheet&quot; href=&quot;styles.css&quot; /&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;a href=&quot;next1.html&quot;&gt;
		&lt;img src=&quot;images/button1_off.gif&quot; width=&quot;113&quot; height=&quot;33&quot; alt=&quot;button1&quot; id=&quot;button1&quot; /&gt;
	&lt;/a&gt;&amp;nbsp;&amp;nbsp;
	&lt;a href=&quot;next2.html&quot;&gt;
		&lt;img src=&quot;images/button2_off.gif&quot; width=&quot;113&quot; height=&quot;33&quot; alt=&quot;button2&quot; id=&quot;button2&quot; /&gt;
		&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>The CSS solution:</p>
<pre class="brush: css;">
body {
	background-color: white;
}
img {
	border-width: 0;
}
</pre>
<p>The JavaScript solution:</p>
<pre class="brush: jscript;">
// initialize the rollovers when window loads
window.onload = rolloverInit;

function rolloverInit() {
	// loop through all images on page
	for (var i = 0; i &lt; document.images.length; i++) {
		// if current image is wrapped in an anchor tag (i.e. &lt;a href=&quot;&quot;&gt;&lt;img&gt;&lt;/a&gt;)
		if (document.images[i].parentNode.tagName == &quot;A&quot;) {
			// initiate rollover setup for this current image, pass the current image to that function
			setupRollover(document.images[i]);
		}
	}
}

// 'thisImage' passed from rolloverInit, meaning we need to setup the rollover for this particular image
function setupRollover(thisImage) {
	// create new child image object for thisImage object's outImage property
	thisImage.outImage = new Image();
	// store current image src as the source for outimage, so it stays same when not rolled over
	thisImage.outImage.src = thisImage.src;
	// capture rollOut function as behavior when thisImage's onmouseout event handler is triggered
	thisImage.onmouseout = rollOut;

	// create new child image object for thisImage object's overImage property
	thisImage.overImage = new Image();
	// store preconsructed '_on'-state image src (using thisImage's captured id/name) as the source for overimage, so it stays changes when rolled over
	thisImage.overImage.src = &quot;images/&quot; + thisImage.id + &quot;_on.gif&quot;;
	// capture rollOver function as behavior when thisImage's onmouseover event handler is triggered
	thisImage.onmouseover = rollOver;
}

function rollOut() {
	// change thisImage's src to the outImage source property (already captured)
	this.src = this.outImage.src;
}

function rollOver() {
	// change thisImage's src to the overImage source property (already captured)
	this.src = this.overImage.src;
}
</pre>
<p>And finally here is a screenshot of the improved end rollover result: </p>
<p>Rollover state A: <div id="attachment_301" class="wp-caption alignleft" style="width: 278px"><img src="http://kirkhings.com/wp-content/uploads/improvedrollovera.png" alt="Improved rollover state A" title="improvedrollovera" width="268" height="73" class="size-full wp-image-301" /><p class="wp-caption-text">Improved rollover state A</p></div></p>
<p>Rollover state B: <div id="attachment_302" class="wp-caption alignleft" style="width: 263px"><img src="http://kirkhings.com/wp-content/uploads/improvedrolloverb.png" alt="Improved rollover state B" title="improvedrolloverb" width="253" height="51" class="size-full wp-image-302" /><p class="wp-caption-text">Improved rollover state B</p></div></p>


<p>Related posts:<ul><li><a href='http://kirkhings.com/code/javascript/three-state-javascript-rollover/312/' rel='bookmark' title='Permanent Link: Three-State JavaScript Rollover'>Three-State JavaScript Rollover</a> <small> photo credit: chimothy27 This programming challenge is from a...</small></li>
<li><a href='http://kirkhings.com/code/javascript/accessible-javascript-rollover-with-three-state-functionality/319/' rel='bookmark' title='Permanent Link: Accessible JavaScript Rollover with Three-State Functionality'>Accessible JavaScript Rollover with Three-State Functionality</a> <small> photo credit: Rhian vK This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/disjointed-javascript-rollover-causes-separate-animation/324/' rel='bookmark' title='Permanent Link: Disjointed JavaScript Rollover Causes Separate Animation'>Disjointed JavaScript Rollover Causes Separate Animation</a> <small> photo credit: Matt McGee This programming challenge is from...</small></li>
<li><a href='http://kirkhings.com/code/javascript/random-image-display-with-javascript/406/' rel='bookmark' title='Permanent Link: Random Image Display with JavaScript'>Random Image Display with JavaScript</a> <small> photo credit: davidChief This programming challenge is from a...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/javascript/simple-and-improved-image-rollovers/298/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Plain and Recursive Factorials with Full Print Enhancements</title>
		<link>http://kirkhings.com/code/java/plain-and-recursive-factorials-with-full-print-enhancements/239/</link>
		<comments>http://kirkhings.com/code/java/plain-and-recursive-factorials-with-full-print-enhancements/239/#comments</comments>
		<pubDate>Sun, 19 Apr 2009 04:00:56 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[factorial]]></category>
		<category><![CDATA[recursive]]></category>
		<category><![CDATA[self-instructional challenge]]></category>
		<category><![CDATA[wibit]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=239</guid>
		<description><![CDATA[ photo credit: gadl
This programming challenge is from a WiBit.net instructional video, which I studied/practiced this spring 2009. The end result itself is not terribly sexy, but I liked the algorithm construction process. My programming curriculum changed course during the first year, and they eliminated our Java classes. I was still curious about Java, so [...]


Related posts:<ul><li><a href='http://kirkhings.com/code/java/bottles-of-beer-song-via-java/388/' rel='bookmark' title='Permanent Link: Bottles of Beer Song via Java'>Bottles of Beer Song via Java</a> <small> photo credit: NUCO This programming challenge is from the...</small></li>
<li><a href='http://kirkhings.com/code/java/phrase-o-matic-consultant-wisdom-via-java/419/' rel='bookmark' title='Permanent Link: Phrase-O-Matic Consultant Wisdom via Java'>Phrase-O-Matic Consultant Wisdom via Java</a> <small> photo credit: schedetzki Challenge: Create three lists of buzzwords...</small></li>
</ul>]]></description>
			<content:encoded><![CDATA[<div class="borrowedPic"><a href="http://www.flickr.com/photos/24183489@N00/279433682/" title="GEB Recursive" target="_blank"><img src="http://farm1.static.flickr.com/91/279433682_23ac618518.jpg" alt="GEB Recursive" border="0" /></a><br /><small><a href="http://creativecommons.org/licenses/by-sa/2.0/" title="Attribution-ShareAlike License" target="_blank"><img src="http://kirkhings.com/wp-content/plugins/photo-dropper/images/cc.png" alt="Creative Commons License" border="0" width="16" height="16" align="absmiddle" /></a> <a href="http://www.photodropper.com/photos/" target="_blank">photo</a> credit: <a href="http://www.flickr.com/photos/24183489@N00/279433682/" title="gadl" target="_blank">gadl</a></small></div>
<p>This programming challenge is from a WiBit.net instructional video, which I studied/practiced this spring 2009. The end result itself is not terribly sexy, but I liked the algorithm construction process. My programming curriculum changed course during the first year, and they eliminated our Java classes. I was still curious about Java, so I took it upon myself to self-learn Java. This was one of my first products.</p>
<p>I post these self-instructional challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it. Finally, self-instructional challenges are interesting and different than academic challenges because I sought, discovered, and performed them on my own volition without anyone else telling me to do so. I did it because I wanted to learn and practice.</p>
<blockquote><p>Challenge: Create a simple factorial calculator in Java, using the NetBeans IDE. The original project simply output something like &#8220;5! = 120&#8243;. I improved it to output each member integer so as to clarify the factorial process, so my program output something like &#8220;5! = 1 x 2 x 3 x 4 x 5 = 120&#8243;. I like to push projects a little farther than the original, just to satisfy myself or to learn more.
</p></blockquote>
<p>My solution:</p>
<pre class="brush: java;">
package factorial;
/**
 * @author kirkster
 */
public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int n;
        int i;
        int factorial = 7;
        int result= 1;
        System.out.print(factorial + &quot;! = &quot;);
        for (i = 1; i &lt;= factorial; i++) {
            System.out.print(i);
            if (i &lt; factorial) {
                System.out.print(&quot; x &quot;);
            }
        }
        for (i = 1; i &lt;= factorial; i++) {
            result = fact(i);
        }
        System.out.print(&quot; = &quot; + result);
        System.out.println(&quot;&quot;);
    }

    static int fact(int n) {
        int i;
        int result = 1;
        for (i = 1; i &lt;= n; i++) {
            result *= i;
        }
        return result;
    }
}
</pre>
<p>After this first factorial program, I did a WiBit video doing the same thing using a recursive algorithm. It accomplishes the same thing but in a computer-scientist-sexier way:</p>
<pre class="brush: java;">
package recursivefactorial;
/**
 * @author kirkster
 */
public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println(factorial(n));
    }

    public static long factorial(int n) {
        if (n &lt; 0)
            return -1;
        else if (n == 0)
            return 1;
        else
            return n * factorial(n -1);
    }
}
</pre>
<p>After doing both programs, I wanted to enhance the recursive version with the full step printouts like I did in the first plain version. It was a bit trickier to implement printing out each member integer like my previous improvement, and I could only print the member integers in backwards format (avoiding an extra loop) but this was my solution:</p>
<pre class="brush: java;">
package recursivefactorial;
/**
 * @author kirkster
 */
public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int n = 7;
        System.out.print(n + &quot;! = &quot;);
        System.out.println(factorial(n));
    }

    public static long factorial(int n) {
        if (n &lt; 0)
            return -1;
        else if (n == 0) {
            System.out.print(&quot; = &quot;);
            return 1;
        }
        else {
            if (n == 1)
                System.out.print(n);
            else
                System.out.print(n + &quot; x &quot;);
            return n * factorial(n -1);
        }
    }
}
</pre>
<p>Here is the output:</p>
<div id="attachment_241" class="wp-caption alignleft" style="width: 356px"><img src="http://kirkhings.com/wp-content/uploads/recursivefactorial.png" alt="Output from a recursive factorial algorithm built in Java" title="recursivefactorial" width="346" height="119" class="size-full wp-image-241" /><p class="wp-caption-text">Output from a recursive factorial algorithm built in Java</p></div>


<p>Related posts:<ul><li><a href='http://kirkhings.com/code/java/bottles-of-beer-song-via-java/388/' rel='bookmark' title='Permanent Link: Bottles of Beer Song via Java'>Bottles of Beer Song via Java</a> <small> photo credit: NUCO This programming challenge is from the...</small></li>
<li><a href='http://kirkhings.com/code/java/phrase-o-matic-consultant-wisdom-via-java/419/' rel='bookmark' title='Permanent Link: Phrase-O-Matic Consultant Wisdom via Java'>Phrase-O-Matic Consultant Wisdom via Java</a> <small> photo credit: schedetzki Challenge: Create three lists of buzzwords...</small></li>
</ul></p>]]></content:encoded>
			<wfw:commentRss>http://kirkhings.com/code/java/plain-and-recursive-factorials-with-full-print-enhancements/239/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
