<?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>NathanHuff.net</title>
	<atom:link href="http://www.nathanhuff.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nathanhuff.net</link>
	<description></description>
	<lastBuildDate>Sun, 08 Apr 2012 03:23:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Recursion</title>
		<link>http://www.nathanhuff.net/recursion/</link>
		<comments>http://www.nathanhuff.net/recursion/#comments</comments>
		<pubDate>Tue, 03 Apr 2012 04:13:33 +0000</pubDate>
		<dc:creator>Nathaniel Huff</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[clever]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[recursion]]></category>

		<guid isPermaLink="false">http://www.nathanhuff.net/?p=36</guid>
		<description><![CDATA[What a fun topic to start my blog off with. This entire week I have been working on solutions at work that involve recursive functions. It all started when I helped Drew with a website that he is testing out. He wanted the background to change color every time the user pressed enter on a search box.

After a quick web search, I came across a post by Paul Irish and it intrigued me. The first solution that he and some friends came up was this:]]></description>
			<content:encoded><![CDATA[<p>What a fun topic to start my blog off with. This entire week I have been working on solutions at work that involve recursive functions. It all started when I helped Drew with a website that he is testing out. He wanted the background to change color every time the user pressed enter on a search box.</p>
<p>After a quick web search, I came across <a title="Random Hex Color Code Snippets" href="http://paulirish.com/2009/random-hex-color-code-snippets/" target="_blank">a post by Paul Irish</a> and it intrigued me. The first solution that he and some friends came up was this:<span id="more-36"></span></p>
<div class="code">
<pre class="javascript" style="font-family: monospace;"><span style="color: #3366cc;">'#'</span> <span style="color: #339933;">+</span> <span style="color: #009900;">(</span><span style="color: #3366cc; font-weight: bold;">function</span> co<span style="color: #009900;">(</span>lor<span style="color: #009900;">)</span><span style="color: #009900;">{</span>   <span style="color: #3366cc; font-weight: bold;">return</span> <span style="color: #009900;">(</span>lor <span style="color: #339933;">+=</span>
  <span style="color: #009900;">[</span><span style="color: #cc0000;">0</span><span style="color: #339933;">,</span><span style="color: #cc0000;">1</span><span style="color: #339933;">,</span><span style="color: #cc0000;">2</span><span style="color: #339933;">,</span><span style="color: #cc0000;">3</span><span style="color: #339933;">,</span><span style="color: #cc0000;">4</span><span style="color: #339933;">,</span><span style="color: #cc0000;">5</span><span style="color: #339933;">,</span><span style="color: #cc0000;">6</span><span style="color: #339933;">,</span><span style="color: #cc0000;">7</span><span style="color: #339933;">,</span><span style="color: #cc0000;">8</span><span style="color: #339933;">,</span><span style="color: #cc0000;">9</span><span style="color: #339933;">,</span><span style="color: #3366cc;">'a'</span><span style="color: #339933;">,</span><span style="color: #3366cc;">'b'</span><span style="color: #339933;">,</span><span style="color: #3366cc;">'c'</span><span style="color: #339933;">,</span><span style="color: #3366cc;">'d'</span><span style="color: #339933;">,</span><span style="color: #3366cc;">'e'</span><span style="color: #339933;">,</span><span style="color: #3366cc;">'f'</span><span style="color: #009900;">]</span><span style="color: #009900;">[</span>Math.<span style="color: #9933dd;">floor</span><span style="color: #009900;">(</span>Math.<span style="color: #9933dd;">random</span><span style="color: #009900;">(</span><span style="color: #009900;">)</span><span style="color: #339933;">*</span><span style="color: #cc0000;">16</span><span style="color: #009900;">)</span><span style="color: #009900;">]</span><span style="color: #009900;">)</span>
  <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">(</span>lor.<span style="color: #9933dd;">length</span> <span style="color: #339933;">==</span> <span style="color: #cc0000;">6</span><span style="color: #009900;">)</span> <span style="color: #339933;">?</span>  lor <span style="color: #339933;">:</span> co<span style="color: #009900;">(</span>lor<span style="color: #009900;">)</span><span style="color: #339933;">;</span> <span style="color: #009900;">}</span><span style="color: #009900;">)</span><span style="color: #009900;">(</span><span style="color: #3366cc;">''</span><span style="color: #009900;">)</span><span style="color: #339933;">;</span></pre>
</div>
<p>It is actually quite a clever solution. My first suggestion was the last one that Paul Irish posted. It involved converting FFFFFF from hexadecimal to decimal and just generating a number from 0-16777215. The solution posted here however picks one of the possible 16 choices that exist in hexadecimal 6 times and concatenates them together to get the random color.</p>
<p>However, I was confused since right at the beginning, this function had a variable called &#8220;lor&#8221; that I had no idea where it was getting initialized. After some of my own testing and dissecting of this code, I learned that after a function header, after the closing curly bracket, you can pass a variable right off the bat. Like his example does the <span style="color: #009900; font-family: monospace;">(</span><span style="color: #3366cc; font-family: monospace;">&#8216;<span></span>&#8216;</span><span style="color: #009900; font-family: monospace;">)</span> right at the end is an empty string that &#8220;lor&#8221; starts off as in this function.</p>
<p>Once it generates the random character the first time around, it checks if &#8220;lor&#8221; is six characters long. If it isn&#8217;t, then it recursively calls itself with the new &#8220;lor&#8221; variable. It is quite the ingenious solution.</p>
<p>In my next blog post, I will have to share the solution that Clay and I came up with to solve a recursive problem.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nathanhuff.net/recursion/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

