<?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>{ Code: Impossible } &#187; Javascript</title>
	<atom:link href="http://codeimpossible.com/tag/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://codeimpossible.com</link>
	<description>this = HowI.Roll();</description>
	<lastBuildDate>Thu, 29 Jul 2010 02:58:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>W.O.M.M. #6 &#8211; enumerateOver()</title>
		<link>http://codeimpossible.com/2010/03/07/w-o-m-m6-enumerateover/</link>
		<comments>http://codeimpossible.com/2010/03/07/w-o-m-m6-enumerateover/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 04:17:52 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jsoq]]></category>
		<category><![CDATA[Open Source]]></category>

		<guid isPermaLink="false">http://codeimpossible.com/?p=478</guid>
		<description><![CDATA[
I&#8217;ve been focusing more and more on  my port of Ling-to-Objects, Jsoq the past few weeks. It&#8217;s still in really early stages and I&#8217;m not quite sure about it&#8217;s actual usefulness but I&#8217;m learning a lot about JavaScript and having a ton of fun along the way!
Jsoq deals with arrays a lot. About 95% [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft" title="works-on-my-machine-starburst" src="http://codeimpossible.com/wp-content/uploads/2009/06/works-on-my-machine-starburst.jpg" alt="works-on-my-machine-starburst" /></p>
<p>I&#8217;ve been focusing more and more on <a href="http://bitbucket.org/codeimpossible/jsoq"> my port of Ling-to-Objects, Jsoq</a> the past few weeks. It&#8217;s still in really early stages and I&#8217;m not quite sure about it&#8217;s actual usefulness but I&#8217;m learning a lot about JavaScript and having a ton of fun along the way!</p>
<p>Jsoq deals with arrays a lot. About 95% of it&#8217;s use cases involve either looping through, altering, or creating arrays. Having a ton of <code>for</code> loops in my code just seems so&#8230; not right. For loops have always seemed dirty to me. They just aren&#8217;t elegant enough. </p>
<p>Here is your normal, average, everyday <code>for</code> loop in Javascript.</p>
<pre class="prettyprint"><code>
var array = "1,2,3,4,5,6,7,8,9,10".split(',');

for(var i = -1, l = array.length; ++i &lt; l;) {
    alert(array[i]);
}
</code></pre>
<p>This works. It&#8217;s reliable and gets the message across. But what if we needed to loop over an array and get all the items that matched a condition? Using a <code>for</code> loop the code could look something like:</p>
<pre class="prettyprint"><code>
var array = [ 1, 2, 3, 4, 5, 1, 2, 3, 4 ];
var results = [];
var condition = function(i) { return i%2 == 0; };

for(var itt = -1, len = array.length; ++itt &lt; len;) {
	if( condition(array[itt]) ) {
		results.push(array[itt]);
	}
}
</code></pre>
<p>This does work, I&#8217;ve written code like this many times before, and while <em>technically</em> there isn&#8217;t anything wrong with it I think there is still room for improvement.</p>
<p>Jsoq is going to be handling arrays all over the place so the solution to this problem <em>needs</em> to be simple.</p>
<p>Here&#8217;s what I need:</p>
<ul>
<li>to loop over an entire collection and perform an action on each item.</li>
<li>If that action produces a result, the item is to be pushed into an array and returned to after the loop is done</li>
<p>.</p>
<li>Also: it needs to be readable, someone else coming along should be able to determine what this thing is doing without too much difficulty. So I had my work cut out for me. </li>
</ul>
<p>A few hours later I had a decent function that I could use to replace a lot of the <code>for</code> loops I had. After some more refactoring I was able to wipe them all out and replace them with calls to <code>enumerateOver()</code>. Here is the latest version from source control:</p>
<pre class="prettyprint"><code>
function enumerateOver(collection, work) {
	var result = [], val = [];

	if (isArray(collection)) {
		try {
			for (var i = -1, l = collection.length; ++i < l;) {
				result = work(collection[i], i);
				if (typeof result !== "undefined" &#038;&#038; result != null) {
					val.push(result);
				}
			}
		}catch (e) {
			if (e != jsoq.$break) throw(e);
		}

		if (val.length > 0) {
			return val;
		}
	}else {
		try {
			val = work(collection, 0);
		}
		catch (e) {
			if (e != jsoq.$break) throw(e);
		}
		if (typeof val !== 'undefined') {
			return val;
		}
	}
	return result == null ? [] : result;
}
</code></pre>
<p>And here is the code to replace the <code>for</code> loops above, re-written to use <code>enumerateOver()</code></p>
<pre class="prettyprint"><code>
var results2 = enumerateOver(array, function(i, c) {
     return i%2 == 0;
});
</code></pre>
<p>So by implmenting this function I was able to come up with a more readable, testable and streamlined codebase. Is this suitable for everyone? Definitely not, but I did it for a few reasons:</p>
<ol>
<li>Like I mentioned before. I&#8217;ve never been at ease with <code>for</code> loops and being able to replace them all with calls to a single function was a huge win for me</li>
<li>The normal use-case didn&#8217;t fit right. I needed to not only iterate over arrays but also return the results of work performed on those items. Doing this the &#8220;regular&#8221; way just wouldn&#8217;t work (see previous reason)</li>
<li>I thought this was a fun problem to solve</li>
</ol>
<p>If you have any feedback, good, bad, or indifferent add a comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://codeimpossible.com/2010/03/07/w-o-m-m6-enumerateover/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using jQuery on an Iframe</title>
		<link>http://codeimpossible.com/2010/03/04/using-jquery-on-an-iframe/</link>
		<comments>http://codeimpossible.com/2010/03/04/using-jquery-on-an-iframe/#comments</comments>
		<pubDate>Fri, 05 Mar 2010 01:10:39 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jquery-extensions]]></category>

		<guid isPermaLink="false">http://codeimpossible.com/?p=803</guid>
		<description><![CDATA[The other day I had to alter the stylesheets in a child IFrame when a user selected an item from a drop-down. My first draft was pretty ugly, it ivolved getting the DOM from the child IFrame (by getting it&#8217;s contentWindow or contentDocument property) then getting the  of the DOM and looping over all [...]]]></description>
			<content:encoded><![CDATA[<p>The other day I had to alter the stylesheets in a child IFrame when a user selected an item from a drop-down. My first draft was pretty ugly, it ivolved getting the DOM from the child IFrame (by getting it&#8217;s <a href="http://www-archive.mozilla.org/docs/dom/domref/dom_frame_ref5.html">contentWindow</a> or <a href="http://www-archive.mozilla.org/docs/dom/domref/dom_frame_ref4.html">contentDocument</a> property) then getting the <head> of the DOM and looping over all the child items&#8230; yuck!</p>
<p>I coded up this jQuery extension method which will return a jQuery-wrapped DOM instance for any of the matched IFrames.</p>
<pre class="prettyprint"><code>
(function($) {
    $.fn.extend({
        dom: function () {
            var $this = $(this);
            var getDom = function(o) {
                if( !o || (!o.contentWindow &#038;&#038; !o.contentDocument) ) {
                    return null;
                }

                var doc = (o.contentWindow || o.contentDocument);

                return doc.document || doc;
            };

            var dom = getDom($this[0]);

            return dom === null ? $this : $(dom);
        }
    });
})(jQuery);
</code></pre>
<p>So with this, getting the styles for a child IFrame is as easy as:</p>
<pre class="prettyprint"><code>
$('iframe').dom().find('head link').each(function(index, item) {
    alert(item.href);
});
</code></pre>
<p><em>Unfortunately, this code will only work if your IFrame is hosting content that is on the same domain as its parent.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://codeimpossible.com/2010/03/04/using-jquery-on-an-iframe/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Solving &#8220;$(document).ready is not a function&#8221; and other problems</title>
		<link>http://codeimpossible.com/2010/01/13/solving-document-ready-is-not-a-function-and-other-problems/</link>
		<comments>http://codeimpossible.com/2010/01/13/solving-document-ready-is-not-a-function-and-other-problems/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 01:29:29 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code-style]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://codeimpossible.com/?p=767</guid>
		<description><![CDATA[Has this ever happened to you: you&#8217;ve been working on a customer&#8217;s site, writing some really awesome jQuery flashy, fadey, scrolly, interactivey thing, you deploy it, and everything is awesome. The customer rejoices and the customer&#8217;s customers rejoice. Rejoicing is had by everyone. 
And then you get an email one day:

&#8220;Everything is broken. We&#8217;ve kidnapped [...]]]></description>
			<content:encoded><![CDATA[<p>Has this ever happened to you: you&#8217;ve been working on a customer&#8217;s site, writing some really awesome jQuery flashy, fadey, scrolly, interactivey thing, you deploy it, and everything is awesome. The customer rejoices and the customer&#8217;s customers rejoice. Rejoicing is had by everyone. </p>
<p>And then you get an email one day:</p>
<blockquote><p>
&#8220;Everything is broken. We&#8217;ve kidnapped your dog. Fix our site or you&#8217;ll never see Spartacus again.&#8221;</p></blockquote>
<p>And before you have time to wonder why you ever named your dog &#8220;Spartacus&#8221; to begin with (i mean <strong>come. on.</strong>), you&#8217;re off in debug hell. You load the site and see all sorts of weird errors:</p>
<p><code>“$().ready is not a function”</code></p>
<p><code>“$(document) doesn’t support this property or method”</code></p>
<p>Or my personal favorite: </p>
<p><code>“null is null or not an object”</code></p>
<p>You open up FireFox, activate FireBug, load the console, and type “alert($)”, press run, and instead of seeing the expected jQuery function:</p>
<pre class="prettyprint"><code>
function (E, F) {
    return new (o.fn.init)(E, F);
}
</code></pre>
<p>You instead get:</p>
<pre class="prettyprint"><code>
function $(element) {
    if (arguments.length > 1) {
        for (var i = 0, elements = [], length = arguments.length; i < length; i++) {
            elements.push($(arguments[i]));
        }
        return elements;
    }
    if (Object.isString(element)) {
        element = document.getElementById(element);
    }
    return Element.extend(element);
}
</code></pre>
<p>Or even:</p>
<pre class="prettyprint"><code>
function $(id) {
    return document.getElementById(id);
}
</code></pre>
<p><strong>DOH!</strong> Looks like another javascript library has been loaded and has overwritten the <code>$()</code> shortcut for jQuery. Woe is I. Why can’t we all just get along?!? </p>
<p>Well, we can’t stop people from including their favorite javascript libraries, but what we can do is prevent our code from suffering as a result. We’ll need a nice, big beefy, bodyguard to make sure our code isn’t messed with while it’s out clubbing with Prototype, Scriptaculous or even MooTools (who invited <em>him</em>??!?).</p>
<p>Here’s what our bodyguard function will look like</p>
<pre class="prettyprint"><code>
( function($) {

} ) ( jQuery );
</code></pre>
<p>So what this does is call our anonymous function and pass the <code>jQuery</code> object. This will scope ‘$’ to within our little function so we won’t step on anyone else’s toes (and they won’t bump into us while we’re on the dance floor and spill our drink everywhere). Okay, I think I've taken the clubbing metaphor far enough.</p>
<p>Basically this will allow our code to run and use the <code>$</code> shortcut for JQuery as if it were loaded without any of these other libraries on the page. </p>
<p>Here is what the completed code would look like:</p>
<pre class="prettyprint"><code>
&lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js" type="text/javascript"&gt;
&lt;/script&gt;

&lt;script src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.1.0/prototype.js" type="text/javascript">
&lt;/script&gt;
&lt;script src="http://ajax.googleapis.com/ajax/libs/scriptaculous/1.8.3/scriptaculous.js" type="text/javascript">
&lt;/script&gt;

&lt;script type="text/javascript"&gt;
( function($) {
    // we can now rely on $ within the safety of our “bodyguard” function
    $(document).ready( function() { alert("nyah nyah! I’m able to use '$'!!!!");  } );
} ) ( jQuery );

//this will fail
$(document).ready( function() { alert('fail?'); } );
&lt;/script&gt;
</code></pre>
<p>I love using this simple self-calling anonymous function style when working with jQuery because it saves me from typing <code>jQuery()</code>, which really does look a lot more ugly than using the <code>$()</code> shortcut. It also protects my code from any scoping issues and lets the code function normally when <a href="http://docs.jquery.com/Core/jQuery.noConflict">jQuery is put into no conflict mode</a>. </p>
<p>My opinion, if you're doing work in jQuery on sites that you don't control 100%, you should be using this method to protect your code and your clients.</p>
<p><strong>Updated: changed link for jquery to use 1.4.1 at the google CDN (tsk, tsk, tsk I was using the googlecode.com link)</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://codeimpossible.com/2010/01/13/solving-document-ready-is-not-a-function-and-other-problems/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>JavaScript Performance Re-match</title>
		<link>http://codeimpossible.com/2009/12/02/javascript-performance-rematch/</link>
		<comments>http://codeimpossible.com/2009/12/02/javascript-performance-rematch/#comments</comments>
		<pubDate>Wed, 02 Dec 2009 06:07:37 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[firefox]]></category>
		<category><![CDATA[google-chrome]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://codeimpossible.com/?p=703</guid>
		<description><![CDATA[Back in 2007 Jeff Atwood ran the top 4 web browsers through the SunSpider JavaScript Benchmark and posted his findings. It&#8217;s been close to 2 years since then and I was curious to see how FireFox 3.5, IE8, IE7 (in Compatibility Mode) and Google Chrome 4.0.249.11 would fair. 
So I did pretty much the same [...]]]></description>
			<content:encoded><![CDATA[<p>Back in 2007 <a href="http://codinghorror.com" title="Jeff Atwood's blog">Jeff Atwood</a> ran the top 4 web browsers through the <a href="http://www2.webkit.org/perf/sunspider-0.9/sunspider.html" title="SunSpider JavaScript Benchmark">SunSpider JavaScript Benchmark</a> and <a href="http://www.codinghorror.com/blog/archives/001023.html" title="The Great Browser JavaScript Showdown">posted his findings</a>. It&#8217;s been close to 2 years since then and I was curious to see how FireFox 3.5, IE8, IE7 (in Compatibility Mode) and Google Chrome 4.0.249.11 would fair. </p>
<p>So I did pretty much the same thing Jeff did and here is what I found:</p>
<p><img src="http://codeimpossible.com/wp-content/uploads/2009/12/JavaScript-Performance.png" alt="JavaScript Performance Graph" title="JavaScript Performance Graph" class="alignnone size-medium wp-image-704" /></p>
<p><small><em>* System specs: Windows 7 64-bit on a Dual-Core 2.53ghz CPU with 4gb of RAM with no browser extensions</em></small></p>
<ol>
<li>Chrome kicks some serious butt over everyone else with <strong>the entire test suite running to completion in under 1 second!!</strong></li>
<li>FireFox 3.5 has some serious improvements coming in at just over 1.5 seconds total, which is about 1/10 the time it took FireFox 2.0</li>
<li>Internet Explorer 8 and Internet Explorer 7 (compat mode) are still bringing up the rear but they had a better showing than the 20+ seconds IE7 took when Jeff ran the test</li>
</ol>
<p><strong>Surprises?</strong><br />
The only thing I found surprising about the results was that if you removed the string test from both IE runs then IE8 in compatibility mode beats IE8 running normally. That doesn&#8217;t seem right to me and I seriously hope JavaScript performance is something that gets addressed in IE9. And by &#8220;addressed&#8221; I mean &#8220;fixed by replacing Trident with WebKit&#8221;.</p>
<p>So, what do these results <em>actually</em> mean? Well I guess it depends. If you use your browser to read blogs and check your gmail then you shouldn&#8217;t really care about these numbers. However, if you&#8217;re a web developer you should be paying strong attention to these numbers and how far they&#8217;ve come in <em>just 2 years</em>.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeimpossible.com/2009/12/02/javascript-performance-rematch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>W.O.M.M. Project Euler #3</title>
		<link>http://codeimpossible.com/2009/11/25/w-o-m-m-project-euler-3/</link>
		<comments>http://codeimpossible.com/2009/11/25/w-o-m-m-project-euler-3/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 06:25:57 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[math]]></category>

		<guid isPermaLink="false">http://codeimpossible.com/?p=666</guid>
		<description><![CDATA[
Here we are with another excellent installment in the &#8220;Works On My Machine&#8221; series where I post some code, some thoughts and hopefully show you something interesting/cool/new.
Today I&#8217;m going to talk briefly about Project Euler problem #3. Problem 3 asks you to find the largest prime factor of the number 600,851,475,143.
My solution is pretty simple [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft" title="works-on-my-machine-starburst" src="http://codeimpossible.com/wp-content/uploads/2009/06/works-on-my-machine-starburst.jpg" alt="works-on-my-machine-starburst" /></p>
<p>Here we are with another excellent installment in the &#8220;Works On My Machine&#8221; series where I post some code, some thoughts and hopefully show you something interesting/cool/new.</p>
<p>Today I&#8217;m going to talk briefly about Project Euler problem #3. Problem 3 asks you to find the largest <a href="http://en.wikipedia.org/wiki/Prime_factor">prime factor</a> of the number 600,851,475,143.</p>
<p>My solution is pretty simple but it works:</p>
<pre class="prettyprint"><code>
var getLargestPrimeFactor = function(num)
{
    var factors = [];
    for ( var f = 2; f &lt; num; f++ )
    {
        if( num%f == 0 )
        {
            factors.push(f);
            num = num /f;
        }
    }
    factors.push(num);
    return factors[factors.length-1];
};

alert( getLargestPrimeFactor(600851475143));
</code></pre>
<p><a title="Try this code!" href="http://beta.jsvudo.com/76cc38" target="_blank">Try this code!</a></p>
<p>You may have noticed that there isn&#8217;t a check for a factors <del>optimus</del> primeness anywhere in there.</p>
<p>Thats because the <code>getLargestPrimeFactor()</code> function is dividing the number by it&#8217;s smallest factor, and the smallest factor for any number is always prime so when we run out of factors we&#8217;ve got the largest prime factor. Yeah, that&#8217;s pretth cool IMO.</p>
<p>I read the discussion threads for problem 3 after I solved it and I was surprised that hardly anyone removed the prime checks from their code.</p>
<p>So, problem 3 is in the bag and I feel pretty good about coming up with the prime check optimization (or lack thereof). A small bit of proof that I know what I&#8217;m doing. Sometimes :D</p>
]]></content:encoded>
			<wfw:commentRss>http://codeimpossible.com/2009/11/25/w-o-m-m-project-euler-3/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Jsoq Console</title>
		<link>http://codeimpossible.com/2009/11/14/jsoqie/</link>
		<comments>http://codeimpossible.com/2009/11/14/jsoqie/#comments</comments>
		<pubDate>Sat, 14 Nov 2009 06:38:28 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jsoq]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://codeimpossible.com/?p=668</guid>
		<description><![CDATA[During the first round of development for JSOQ I needed a faster way to test than unit-tests alone could provide. What I needed was a dirt-simple page that I could run in a browser, enter some JavaScript code, click a button and see it execute. 
I built the initial JSOQ Console (which is still included [...]]]></description>
			<content:encoded><![CDATA[<p>During <a href="http://codeimpossible.com/2009/02/10/introducing-the-javascript-object-query-library">the first round of development for JSOQ</a> I needed a faster way to test than unit-tests alone could provide. What I needed was a dirt-simple page that I could run in a browser, enter some JavaScript code, click a button and see it execute. </p>
<p>I built the initial JSOQ Console (which is still included with the JSOQ source code) in about 20 minutes and it increased my productivity by 10x.</p>
<p>It was much faster to test JSOQ functions with the JSOQ Console than it had been with unit-tests. As a result I was able to prototype new functionality in about 1/2 the time.</p>
<p>I&#8217;ve finally moved <a href="http://codeimpossible.com/jsoq">v0.2 of the Jsoq Console</a> to my blog here so you can use it to quickly test out your own JavaScript code!</p>
<p>How it works is pretty straight forward. If you&#8217;ve got a snippet of JavaScript, be it jQuery (for the moment jQuery is the only external library that is supported but that will change soon), JSOQ or any other javascript code, you can run it with the JSOQ Console.</p>
<p>Just navigate to <a href="http://tinyurl.com/jsoqconsole">http://tinyurl.com/jsoqconsole</a> or <a href="http://codeimpossible.com/jsoq">http://codeimpossible.com/jsoq</a> for those of you that like to type, paste your code into the textarea at the top of the page, press &#8220;Run&#8221; and your code will run!</p>
<p>So enjoy! If you get confused there are also samples that will show you how to use the more &#8220;advanced&#8221; features. Also, drop a comment or share the console with others if you thought it was useful!</p>
]]></content:encoded>
			<wfw:commentRss>http://codeimpossible.com/2009/11/14/jsoqie/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>W.O.M.M. #5 Project Euler &#8211; Problem 2</title>
		<link>http://codeimpossible.com/2009/09/11/w-o-m-m-5-project-euler-problem-2/</link>
		<comments>http://codeimpossible.com/2009/09/11/w-o-m-m-5-project-euler-problem-2/#comments</comments>
		<pubDate>Fri, 11 Sep 2009 05:49:51 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://codeimpossible.com/?p=639</guid>
		<description><![CDATA[
Project Euler is an awesome website that I found out about a while back via this question on StackOverflow. Basically, it&#8217;s a website that poses a ton of programmer related challenges and it keeps track of the problems that you solve.
The problems start out relatively easy (Add all the natural numbers below one thousand that [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignleft" title="works-on-my-machine-starburst" src="http://wpup.codeimpossible.com/2009/06/works-on-my-machine-starburst.jpg" alt="works-on-my-machine-starburst" /></p>
<p><a href="http://projecteuler.net/">Project Euler</a> is an awesome website that I found out about a while back via <a href="http://stackoverflow.com/questions/4002/code-katas">this question on StackOverflow</a>. Basically, it&#8217;s a website that poses a ton of programmer related challenges and it keeps track of the problems that you solve.</p>
<p>The problems start out relatively easy (Add all the natural numbers below one thousand that are multiples of 3 or 5.) and progress all the way to insane (Caradano Triplets and Covex Holes).</p>
<p>I&#8217;m on the easy side, I just started working through them tonight and thought it would be cool to post how I build the solution to each problem as I complete them.</p>
<p><a href="http://projecteuler.net/index.php?section=problems&#038;id=2">Problem 2</a> is pretty easy:</p>
<p><i>&#8220;Find the sum of all the even-valued terms in the Fibonacci sequence which do not exceed four million.&#8221;</i></p>
<p>So first things first, I&#8217;ll need to pick a language to use to solve this. I chose javascript simply because it was something I knew I could get running pretty quickly. </p>
<p>Next I&#8217;ll need some variables. (note: if you&#8217;re not familiar with Fibonacci numbers <a href="http://en.wikipedia.org/wiki/Fibonacci_number">Wikipedia has an awesome article on them</a>.)</p>
<pre><code class="prettyprint">
var start = 1, end = 4000000, current = 1, previous = 1, sum = 0, temp = 0;
</code></pre>
<p>That should do. I&#8217;ve got my start variable (dropping the 0 in favor of more readable code), my end counter, and my &#8220;where-am-i&#8221; variables: current, previous, temp and my sum variable.</p>
<p>Obviously we&#8217;ll need some kind of loop. How about a while loop?</p>
<pre class="prettyprint"><code>
while( current <= end )
{

}
</code></pre>
<p>Cool. Now when the loop is running I'll need a way to keep track of the current value, the previous value and I need to add the current value to the sum. So....</p>
<pre class="prettyprint"><code>
while( current <= end )
{
    temp = current;
    current = current + previous;
    sum += current;
    previous = temp;
}
</code></pre>
<p>And there we hav... Oh Wait!</p>
<p>I overlooked a pretty important part of the problem description: <b><i>"sum of all the even-valued terms"</i></b>!!!</p>
<p>So given this new(ish) piece of info my solution end sup being:</p>
<pre class="prettyprint"><code>
var start = 1, end = 4000000, current = 1, previous = 1, sum = 0, temp = 0;

while( current <= end )
{
	temp = current;
	current = current + previous;

	if( current%2 == 0 )
		sum += current;

	previous = temp;
}

alert(sum);
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://codeimpossible.com/2009/09/11/w-o-m-m-5-project-euler-problem-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introducing The JavaScript Object Query Library</title>
		<link>http://codeimpossible.com/2009/02/10/introducing-the-javascript-object-query-library/</link>
		<comments>http://codeimpossible.com/2009/02/10/introducing-the-javascript-object-query-library/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 06:51:16 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jsoq]]></category>
		<category><![CDATA[Open Source]]></category>

		<guid isPermaLink="false">http://codeimpossible.wordpress.com/?p=272</guid>
		<description><![CDATA[So, yeah I originally started writing this article back in september when I was about mid-way through JSOQ. Some things have changed since then and I&#8217;ve tried to keep the post up-to-date. Enjoy!
Also, another similar library has come out recently: JSINQ which is a really feature-rich LINQ to Object implementation in Javascript. Really bad-ass stuff.
The Elevator [...]]]></description>
			<content:encoded><![CDATA[<blockquote><p><em>So, yeah I originally started writing this article back in september when I was about mid-way through JSOQ. Some things have changed since then and I&#8217;ve tried to keep the post up-to-date. Enjoy!</em></p>
<p><em>Also, another similar library has come out recently: <a href="http://www.codeplex.com/jsinq" target="_blank">JSINQ</a> which is a really feature-rich LINQ to Object implementation in Javascript. Really bad-ass stuff.</em></p></blockquote>
<p>The Elevator Pitch:</p>
<p>JSOQ is an open-source pet project of mine that lets you easily access the data that you need from large collections in JavaScript. It was developed over a two week period in September of 2008 and is currently at version 1.1.</p>
<p>Everyone probably just shrugged and said &#8220;so??&#8221; Well let me show you. </p>
<p>Lets assume that you have the following JSON string from another web application like, say <a title="Twitter search results in JSON" href="http://search.twitter.com/search.json?q=codeimpossible" target="_blank">twitter search results</a> for example:</p>
<pre class="brush:javascript">
{"results":[
{
"text":"Just another day at the office",
"to_user_id":null,
"from_user":"codeimpossible",
"id":1194587890,
"from_user_id":1316793,
"iso_language_code":"en",
"profile_image_url":"www.path.to.image.us",
"created_at":"Tue, 10 Feb 2009 05:47:33 +0000"
},
{
"text":"@spolsky thats easy! http:\/\/tinyurl.com\/2z42bs",
"to_user_id":1357501,
"to_user":"spolsky",
"from_user":"codeimpossible",
"id":1194582705,
"from_user_id":1316793,
"iso_language_code":"en",
"profile_image_url":"www.path.to.image.us",
"created_at":"Tue, 10 Feb 2009 05:44:51 +0000"
}
]} 
</pre>
<p>In the sample above we only have two results but JSOQ has been tested to work with collections of tens of thousands of items.</p>
<p>The JSON object we have here is perfectly fine if we watned to show a whole bunch of data to the end user, but what if I only wanted to show the search results after a certain date? Or what if I only wanted the text from each message that was typed to this &#8220;spolsky&#8221; character?</p>
<p>To accomplish this in &#8220;straight&#8221; javascript I&#8217;d be looking at writing a lot of loops and if/else code and then I would still have all these extra properties that I don&#8217;t even need.</p>
<p>JSOQ allows me to query a javascript object or collection of objects using a specified criteria and return only the properties/methods I want.</p>
<p>So lets go with the second option. Let&#8217;s get all the .text members from all the messages that were sent to the &#8220;spolsky&#8221; user.</p>
<pre class="brush:javascript">
var search_results = eval(our_json_result);
var result = null;
with(jsoq) {
    result = From(search_results.results)
                .query('text')
                .where(function(i) {
                    return i.to_user === "spolsky";
                });
}
</pre>
<p> </p>
<p>And now our result object is filled with a bunch of other objects that would look something like:</p>
<pre class="brush:javascript">
{
    text: "@spolsky thats easy! http:\/\/tinyurl.com\/2z42bs"
}
</pre>
<p> </p>
<p>Pretty simple eh? For more information on JSOQ please check back here in the near future or contribute to <a title="JSOQ Google Code Page" href="http://code.google.com/p/jsoq/" target="_blank">the google code repository</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeimpossible.com/2009/02/10/introducing-the-javascript-object-query-library/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Dear &quot;Intarwebz&quot;</title>
		<link>http://codeimpossible.com/2008/09/10/dear-intarwebz/</link>
		<comments>http://codeimpossible.com/2008/09/10/dear-intarwebz/#comments</comments>
		<pubDate>Thu, 11 Sep 2008 03:14:27 +0000</pubDate>
		<dc:creator>Jared</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[google-chrome]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[web browser]]></category>

		<guid isPermaLink="false">http://pistalwhipped.wordpress.com/?p=206</guid>
		<description><![CDATA[It&#8217;s called &#8220;Chrome&#8221;. It happens to be made by Google. It&#8217;s just. another. browser. Are we done? Can we move on as a society?
EDIT: ok, so Chrome is fast. Really fast. I loaded Oliver Hunt&#8217;s JavaScript Ray Tracer in FireFox 3.0.3 and it took 30.393 seconds to complete a render. Under Chrome the render took [...]]]></description>
			<content:encoded><![CDATA[<p><a title="just. another. browser." href="http://www.google.com/chrome" target="_blank">It&#8217;s called &#8220;Chrome&#8221;</a>. It happens to be made by Google. It&#8217;s <strong>just. another. browser.</strong> Are we done? Can we move on as a society?</p>
<p>EDIT: ok, so Chrome is fast. Really fast. I loaded <a href="http://nerget.com/rayjs/rayjs.html" target="_blank">Oliver Hunt&#8217;s JavaScript Ray Tracer</a> in FireFox 3.0.3 and it took 30.393 seconds to complete a render. Under Chrome the render took less than 6.5 seconds.</p>
]]></content:encoded>
			<wfw:commentRss>http://codeimpossible.com/2008/09/10/dear-intarwebz/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
