<?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; algorithm</title>
	<atom:link href="http://kirkhings.com/tag/algorithm/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>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>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>
		<item>
		<title>Random BINGO Card Generator with Ajax Reset</title>
		<link>http://kirkhings.com/code/javascript/random-bingo-card-generator-with-ajax-reset/224/</link>
		<comments>http://kirkhings.com/code/javascript/random-bingo-card-generator-with-ajax-reset/224/#comments</comments>
		<pubDate>Fri, 17 Apr 2009 17:17:10 +0000</pubDate>
		<dc:creator>Kirk</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[lynda]]></category>
		<category><![CDATA[randomizer]]></category>
		<category><![CDATA[self-instructional challenge]]></category>

		<guid isPermaLink="false">http://kirkhings.com/?p=224</guid>
		<description><![CDATA[ photo credit: Keees
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/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/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/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/54366416@N00/6443146/" title="bingo" target="_blank"><img src="http://farm1.static.flickr.com/6/6443146_de0b591435.jpg" alt="bingo" 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/54366416@N00/6443146/" title="Keees" target="_blank">Keees</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 BINGO card using HTML and JavaScript. Generate random integers between 1 and 75, and insert them into each of 24 squares, skipping the center FREE space. Integer ranges are as follow: B = 1-15, I = 16-30, N = 31-45, G = 46-60, O = 67-75. Ensure no two squares contain duplicate numbers. Bonus: After initial card load, enable resetting the card without a server call (quasi-Ajax-powered).
</p></blockquote>
<p>The HTML solution:</p>
<pre class="brush: xml;">
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;Make Your Own Bingo Card&lt;/title&gt;
	&lt;link rel=&quot;stylesheet&quot; rev=&quot;stylesheet&quot; href=&quot;script.css&quot; /&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;script.js&quot;&gt;
	&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Create A Bingo Card&lt;/h1&gt;
&lt;table&gt;
	&lt;tr&gt;
		&lt;th width=&quot;20%&quot;&gt;B&lt;/th&gt;
		&lt;th width=&quot;20%&quot;&gt;I&lt;/th&gt;
		&lt;th width=&quot;20%&quot;&gt;N&lt;/th&gt;
		&lt;th width=&quot;20%&quot;&gt;G&lt;/th&gt;
		&lt;th width=&quot;20%&quot;&gt;O&lt;/th&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td id=&quot;square0&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square1&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square2&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square3&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square4&quot;&gt;&amp;nbsp;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td id=&quot;square5&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square6&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square7&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square8&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square9&quot;&gt;&amp;nbsp;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td id=&quot;square10&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square11&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;free&quot;&gt;Free&lt;/td&gt;
		&lt;td id=&quot;square12&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square13&quot;&gt;&amp;nbsp;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td id=&quot;square14&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square15&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square16&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square17&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square18&quot;&gt;&amp;nbsp;&lt;/td&gt;
	&lt;/tr&gt;
	&lt;tr&gt;
		&lt;td id=&quot;square19&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square20&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square21&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square22&quot;&gt;&amp;nbsp;&lt;/td&gt;
		&lt;td id=&quot;square23&quot;&gt;&amp;nbsp;&lt;/td&gt;
	&lt;/tr&gt;
&lt;/table&gt;
&lt;p&gt;&lt;a href=&quot;script.html&quot; id=&quot;reload&quot;&gt;Click here&lt;/a&gt; to create a new card&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>The CSS solution:</p>
<pre class="brush: css;">
body {
	background-color: white;
	color: black;
	font-size: 20px;
	font-family: &quot;Lucida Grande&quot;, Verdana, Arial, Helvetica, sans-serif;
}

h1, th {
	font-family: Georgia, &quot;Times New Roman&quot;, Times, serif;
}

h1 {
	font-size: 28px;
}

table {
	border-collapse: collapse;
}

th, td {
	padding: 10px;
	border: 2px #666 solid;
	text-align: center;
	font-size: 24px;
}

#free {
	background-color: #ffff00;
}
</pre>
<p>The JavaScript solution:</p>
<pre class="brush: jscript;">
window.onload = initializePage;

// even though arrays are 0-based, creating 76 positions generates elements 0-75, we will later ignore position 0
// note that newly created arrays have all elements set to false
var usedNumbers = new Array(76);

function initializePage() {
	// so older browsers do not get 24 error messages
	if (document.getElementById) {
		// enable creating a new card without reloading page
		document.getElementById(&quot;reload&quot;).onclick = anotherCard;
		// generate the new bingo card
		newCard();
	} else {
		alert(&quot;Sorry, but your browser is not running JavaScript, and this game requires it.&quot;);
	}
}

function newCard() {
	for (var i = 0; i &lt; 24; i++) {
		// pass the current i as parameter to setSquare function
		setSquare(i);
	}
}

function setSquare(thisSquare) {
	// capture current corresponding html square (via id=)  to current digit that was passed over
	var currentSquare = &quot;square&quot; + thisSquare;

	// while bingo numbers are ranged 1-75, letter columns are restricted to specific number ranges
	// b = 1-15, i = 16-30, n = 31-45, g = 46-60, o = 67-75
	// OR mathematically we can generate a random number between 1-15 and add column-specific number
	// b = 0, i = 15, n = 30, g = 45, o = 60	

	// need 5 sets of numbers 0-4 (1 set for each letter)
	// note missing '2,' from third set, so as to skip the middle 'free' square
	var columnPlace = new Array(0,1,2,3,4,0,1,2,3,4,0,1,3,4,0,1,2,3,4,0,1,2,3,4);

	// capture appropriate column mulitple by referencing current square # as array element
	var columnMultiple = (columnPlace[thisSquare] * 15);

	// initialize variable to be used in following do-while loop
	var newNumber;

	// generate newNumber and check if it has been used already, if so loop again
	do {
		// add random number to the particular column multiple
		newNumber = columnMultiple + getNewNumber()  + 1;
	} while(usedNumbers[newNumber]);

	// prevent duplicate numbers by testing if the newNumber has already been used once on this card
	if (!usedNumbers[newNumber]) {
		// if duplicate was not found, this must be an original #, so set this element to true
		usedNumbers[newNumber] = true;

		// assign randomized newNum to current square
		document.getElementById(currentSquare).innerHTML = newNumber;
	}
}

function getNewNumber() {
	// capture random number between 1 and 15 for bingo values
		// math.random() returns random number between 0 and 1
		// math.floor() returns floor of a decimal number (ex. 1.75 becomes 1)
	return Math.floor(Math.random() * 15)
}

function anotherCard() {
	// housekeeping: reset the usedNumbers flag arrays to all false
	for (var i = 1; i &lt; usedNumbers.length; i++) {
		usedNumbers[i] = false;
	}

	// generate the new bingo card
	newCard();
	return false;
}
</pre>
<p>And finally here is a screenshot of the end result: </p>
<div id="attachment_231" class="wp-caption alignleft" style="width: 400px"><img src="http://kirkhings.com/wp-content/uploads/bingocard.png" alt="BINGO card screenshot" title="bingocard" width="390" height="417" class="size-full wp-image-231" /><p class="wp-caption-text">BINGO card screenshot</p></div>


<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/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/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/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/random-bingo-card-generator-with-ajax-reset/224/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
