<?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>The True Tribe &#187; Search Results  &#187;  browser quirks</title>
	<atom:link href="http://www.thetruetribe.com/?s=browser+quirks&#038;feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.thetruetribe.com</link>
	<description>Vroom! That&#039;s us leaving IE in the dust.</description>
	<lastBuildDate>Fri, 22 Jan 2010 23:34:03 +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>Firefox 3.5 Supports Word-Break Break-Word</title>
		<link>http://www.thetruetribe.com/2009/08/firefox-3-5-supports-word-break-break-word/</link>
		<comments>http://www.thetruetribe.com/2009/08/firefox-3-5-supports-word-break-break-word/#comments</comments>
		<pubDate>Mon, 10 Aug 2009 04:53:25 +0000</pubDate>
		<dc:creator>jdempcy</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Firefox]]></category>
		<category><![CDATA[firefox 3.5]]></category>

		<guid isPermaLink="false">http://www.thetruetribe.com/?p=214</guid>
		<description><![CDATA[Ever had a situation where you are dealing with long unbroken strings? For example, say you have to display user-entered web URLs. Sometimes the user will paste in a really long URL, and it keeps overflowing the container. You can make the parent container overflow: hidden but then it will crop the URL. A potentially [...]]]></description>
			<content:encoded><![CDATA[<p>Ever had a situation where you are dealing with long unbroken strings? For example, say you have to display user-entered web URLs. Sometimes the user will paste in a really long URL, and it keeps overflowing the container. You can make the parent container overflow: hidden but then it will crop the URL. A potentially better solution is to force the browser to introduce line-breaks mid-word.</p>
<p>This has always been difficult to achieve cross-browser until now. Unlike most cross-browser quirks, this was one where Interenet Explorer actually had it right. Since IE 5.5, you have been able to use <em>word-wrap: break-word </em>to cause this behavior. Now, finally, Firefox has caught on. As of version 3.5, Firefox now supports this CSS property as well.</p>
<p><strong><em>Here is the recommended code to cause this behavior:<br />
</em></strong></p>
<p>white-space: pre-wrap;       /* css-3 */<br />
white-space: -pre-wrap;      /* Opera 4-6 */<br />
white-space: -o-pre-wrap;    /* Opera 7 */<br />
word-wrap: break-word;       /* Internet Explorer 5.5+ and Firefox 3.5+ */</p>
<p>Source: <a href="http://petesbloggerama.blogspot.com/2007/02/firefox-ie-word-wrap-word-break-tables.html">UnBlog</a></p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.thetruetribe.com%2F2009%2F08%2Ffirefox-3-5-supports-word-break-break-word%2F&amp;linkname=Firefox%203.5%20Supports%20Word-Break%20Break-Word"><img src="http://www.thetruetribe.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.thetruetribe.com/2009/08/firefox-3-5-supports-word-break-break-word/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JavaScript Challenge: Dispense Change</title>
		<link>http://www.thetruetribe.com/2008/05/javascript-challenge-dispense-change/</link>
		<comments>http://www.thetruetribe.com/2008/05/javascript-challenge-dispense-change/#comments</comments>
		<pubDate>Tue, 13 May 2008 00:55:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[MooTools]]></category>

		<guid isPermaLink="false">http://wordpress.thetruetribe.com/?p=25</guid>
		<description><![CDATA[
This challenge is to write a function that takes an amount of change and returns a String Array with the coins to dispense.
Examples:

.25 returns ['quarter']
.26 returns ['quarter', 'penny']
.30 returns ['quarter', 'nickel']
1.01 returns ['dollar', 'penny']
1.16 returns ['dollar', 'dime', 'nickel', 'penny']

One of the requirements is that this returns the least amount of units of currency as possible. [...]]]></description>
			<content:encoded><![CDATA[<div>
<p>This challenge is to write a function that takes an amount of change and returns a String Array with the coins to dispense.<span id="more-25"></span></p>
<p>Examples:</p>
<ul>
<li>.25 <em>returns</em> ['quarter']</li>
<li>.26 <em>returns</em> ['quarter', 'penny']</li>
<li>.30 <em>returns </em>['quarter', 'nickel']</li>
<li>1.01 <em>returns</em> ['dollar', 'penny']</li>
<li>1.16 <em>returns </em>['dollar', 'dime', 'nickel', 'penny']</li>
</ul>
<p>One of the requirements is that this returns the least amount of units of currency as possible. In other words, you can&#8217;t return 100 pennies or even 4 quarters when 1 dollar would suffice. This is modeled after how real-life vending machines work.</p>
<div style="float: left; width: 240px; margin-right: 18px; margin-bottom: 4px;"><a title="Land of the Vending Machines" href="http://www.flickr.com/photos/gullevek/340562157/"><img class="pc_img" src="http://farm1.static.flickr.com/166/340562157_921a63ed9e_m.jpg" alt="Land of the Vending Machines" width="240" height="160" /></a><br />
<span style="font-size:10px;">Land of the Vending Machines      By <a style="text-decoration: none;" href="http://www.flickr.com/photos/gullevek/">gullevek</a></span></div>
<p>If you want to attempt to solve this challenge yourself, <strong>read no further!</strong> The rest of the article will go on to describe my solution to this problem and some of the issues I encountered along the way.</p>
<h3 style="clear: left;">Using MooTools&#8217; <em>round()</em> Method</h3>
<p>I originally wrote my solution to this challenge without the use of JavaScript libraries. However, I ran into a common problem when dealing with dollar values, which is that I needed to round them to the 2nd decimal place. Without rounding to the 2nd place, you end up with subtle flaws in the data.</p>
<p>For instance, I was using an if() statement to check if the change remaining to dispense is greater than or equal to 0.1 (one cent). Before I added the code to round to the 2nd decimal place, the if() statement wouldn&#8217;t evaluate to true when there should be a penny left to dispense. The change remaining should have been .1, but it wasn&#8217;t entering the <span style="font-style:italic;">if(changeToDispense &gt;= 0.1)</span> statement. How could this be? Well, I was subtracting the change, but upon closer inspection, some of the values were not what I expected. When I subtract .25 from .48, I ended up with 0.22999999999999998 for instance.</p>
<p>Of course, I should have ended up with 0.23, not .022999 (repeating). But, without rounding the number, it ended up slightly less than the correct amount, introducing subtle bugs into the code. Since the full amount wasn&#8217;t being subtracted, the last penny would never be dispensed because there wasn&#8217;t a whole penny left (it was something like 0.09999999999999998). Thus, I rounded to the second decimal place and it solved the problem.</p>
<p>Using core JavaScript methods, it&#8217;s possible to work around this problem. It involves multiplying by 100, rounding and then dividing by 100. But, since most of the projects I work on use JavaScript libraries and nearly all of them have round() functions in some form or another, I decided to get a little help from a library. Specifically, I use the MooTools <em>round()</em> method for solving this problem.</p>
<p>The <em>round()</em> method takes how many places after the decimal to round to, so by calling <em>round(2)</em>, a number like 0.0999 (repeating) is converted to 0.1.</p>
<div style="float: left; margin-right: 18px; margin-bottom: 4px; width: 180px;"><span class="photo_container pc_m"><a title="Vending Machine - Japan" href="http://www.flickr.com/photos/rytc/345331437/"><img class="pc_img" src="http://farm1.static.flickr.com/132/345331437_7da8fd7cfc_m.jpg" alt="Vending Machine - Japan" width="180" height="240" /></a></span> <span style="font-size: 10px;"> Vending Machine &#8211; Japan<br />
By <a style="text-decoration: none;" href="http://www.flickr.com/photos/rytc/">rytc</a></span></div>
<p>Note that as the method is added to the prototype of the Number object (i.e. base class), it is called as an instance method of the number you want to round, <em>not</em> as a method of the Math object as with core JS methods. Compare:</p>
<ul>
<li><strong>Core JS:</strong> Math.round(1.8);</li>
<li><strong>MooTools:</strong> 1.8.round();</li>
</ul>
<h3 style="clear: left;">Designing a Solution</h3>
<p>My idea for the solution is to store a hash of the coin values keyed by their name, and then iterate over them checking if each value is higher than the amount of remaining change to dispense. For this to work, I need to iterate over them in order from highest to lowest. Otherwise, if I start in random order, it will break one of the requirements: This must return the least units of currency as possible. If I start at the penny, then it would keep dispensing pennies until there was no change left, and never get on to the other units of currency.</p>
<p>Therefore, the loop needs to <strong>iterate over the currency values in order of highest to lowest</strong>: dollar, quarter, dime, nickel, penny. Here is the hash object that I created to represent the currency values, and then we&#8217;ll discuss how to iterate over them in order:</p>
<pre class="js:nocontrols"> var money =
     'dollar': 1.00,
     'quarter': 0.25,
     'dime': 0.10,
     'nickel': 0.05,
     'penny': 0.01
 };</pre>
<p>You might think it&#8217;s as easy as using a for/in loop, but unfortunately, browser compatibility issues prevent it from being this simple. Using a for/in loop will iterate over the items but there is no guarantee of the order. Internet Explorer and Firefox use the correct order (the order of declaration) as far as I know, but Safari in particular does not follow this. So, it&#8217;s not as simple as:</p>
<pre class="js:nocontrols">function dispenseChange(changeToDispense) {
 var result = new Array();
 for (var unit in money) {
     if (money[unit] &gt; changeToDispense) result.push(unit);
 }
 return result;
}</pre>
<p>It would be nice if it were this simple, and indeed in most browsers it is. But due to obscure quirks with the order of iteration over objects of properties, we need to store the order in its own array.</p>
<div style="float: left; margin-right: 18px; margin-bottom: 4px; width: 180px;"><span class="photo_container pc_m"><a title="Asahi Beer Vending Machine: Komatsu" href="http://www.flickr.com/photos/jpellgen/444636731/"><img class="pc_img" src="http://farm1.static.flickr.com/234/444636731_42117bff1b_m.jpg" alt="Asahi Beer Vending Machine: Komatsu" width="180" height="240" /></a></span> <span style="font-size:10px;"> Asahi Beer Vending Machine: Komatsu<br />
By <a style="text-decoration: none;" href="http://www.flickr.com/photos/jpellgen/">jpellgen</a><br />
</span></div>
<h3 style="clear:both;">A Working Solution</h3>
<p>I&#8217;ve changed the <em>money</em> object to now contain two properties, a <em>values</em> hash and an <em>order</em> array.</p>
<pre class="js:nocontrols"> var money = {
     values: {
         'dollar': 1.00,
         'quarter': 0.25,
         'dime': 0.10,
         'nickel': 0.05,
         'penny': 0.01
     },
     order: ['dollar', 'quarter', 'dime', 'nickel', 'penny']
 };</pre>
<p>Now I can iterate over the <em>order</em> array and use that to key the <em>values</em> hash. It&#8217;s one extra step in the process but it doesn&#8217;t add too much bloat to the code, and it actually works (at least, in the browsers I&#8217;ve tested):</p>
<pre class="js:nocontrols">function dispenseChange(changeToDispense) {
 var result = new Array();
 while (changeToDispense.round(2) &gt;= 0.01) {
     for (var i = 0; i &lt; money.order.length; i++) {
         var value = money.values[money.order[i]]; // Number, e.g. 1.00
         if (changeToDispense.round(2) &gt;= value) {
             result.push(money.order[i]); // String, e.g. "dollar"
             changeToDispense -= value;
             break;
         }
     }
 }
 return result;
}</pre>
<p>You can test the function with logging statements:</p>
<pre class="js:nocontrols">window.addEvent('domready', function() {
 var tests = [.01, .05, .11, .27, .74, 1.08, 1.99, 2.07];
 tests.each(function(testValue) {
     console.log(testValue.toString() +
                 ': ' +
                 dispenseChange(testValue)
     );
 });
});</pre>
<h3 style="clear: left;">Improving the Implementation</h3>
<p>One area for improvement is the nested for() loop inside the while() loop. Right now it isn&#8217;t ideal, since it will waste iterations checking for currency amounts higher than the current currency. To explain what I mean, let&#8217;s look at the loop again:</p>
<pre class="js:nocontrols"> while (changeToDispense.round(2) &gt;= 0.01) {
     for (var i = 0; i &lt; money.order.length; i++) {
         // ...
         if (changeToDispense.round(2) &gt;=
             money.values[money.order[i]) {
             // dispense unit of currency
             break;
         }
     }
 }</pre>
<p>I commented out some of the implementation details so we can focus on the structure. The reason that this is sub-optimal is that after each unit of currency is dispensed, the for() loop breaks and the while() loop starts it over again. Say there is less than a dollar remaining to dispense&#8211; for each unit of currency dispensed, the for() loop will still check for a dollar.</p>
<p>To optimize this code, we&#8217;d have to only iterate over the <em>order</em> array once. This means the for() loop would be outside the while() loop, and for each unit of currency, the while() loop would continue to dispense change of that unit until the change remaining is less than the unit&#8217;s value. It would be optimized to <em>n</em> (see <a href="http://en.wikipedia.org/wiki/Big_O_notation">Big O notation</a> for details) at that point.</p>
<h3>Optimized Code</h3>
<p>Here is the <strong>new and improved</strong> code with the optimized loops:</p>
<pre class="js">var money = {
 values: {
     'dollar': 1.00,
     'quarter': 0.25,
     'dime': 0.10,
     'nickel': 0.05,
     'penny': 0.01
 },
 order: ['dollar', 'quarter', 'dime', 'nickel', 'penny']
};
function dispenseChange(changeToDispense) {

 var result = new Array();
 money.order.each(function(unit, i) {
     var value = money.values[unit]; // Number, e.g. 1.00
     while (changeToDispense.round(2) &gt;= value) {
         result.push(unit); // String, e.g. "dollar"
         changeToDispense -= value;
     }
 });
 return result;
}

window.addEvent('domready', function() {
 var tests = [.01, .05, .11, .27, .74, 1.08, 1.99, 2.07];
 tests.each(function(testValue) {
     console.log(testValue.toString() +
                 ': ' +
                 dispenseChange(testValue)
     );
 });
});</pre>
<p>An observant reader will notice that I changed the for() loop to use MooTools&#8217; each() method. The reason I did this is that we no longer need to break out of the for() loop, and I prefer the each() notation. (Thanks for recommending it, Lowell!)</p>
<p>As always, feel free to post your own solutions to this challenge in the comments.</p>
<div><span class="photo_container pc_m"><a title="The latest in Japanese vending machines" href="http://www.flickr.com/photos/fotopakismo/2228874582/"><img class="pc_img" src="http://farm3.static.flickr.com/2414/2228874582_8494e45bda_m.jpg" alt="The latest in Japanese vending machines" width="160" height="240" /></a></span></div>
<p><span style="font-size:10px;"> The latest in Japanese vending machines<br />
By <a style="text-decoration: none;" href="http://www.flickr.com/photos/fotopakismo/">El Fotopakismo</a></span></div>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.thetruetribe.com%2F2008%2F05%2Fjavascript-challenge-dispense-change%2F&amp;linkname=JavaScript%20Challenge%3A%20Dispense%20Change"><img src="http://www.thetruetribe.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.thetruetribe.com/2008/05/javascript-challenge-dispense-change/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using MooTools&#8217; Hash.Cookie API</title>
		<link>http://www.thetruetribe.com/2008/05/using-mootools-hash-cookie-api/</link>
		<comments>http://www.thetruetribe.com/2008/05/using-mootools-hash-cookie-api/#comments</comments>
		<pubDate>Thu, 08 May 2008 08:01:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[MooTools]]></category>

		<guid isPermaLink="false">http://wordpress.thetruetribe.com/?p=23</guid>
		<description><![CDATA[
MooTools makes working with cookies quite easy. Based on functions from Paul Peter Koch&#8217;s QuirksMode, the MooTools API for handling cookies is intuitive and easy to remember.
For storing simple bits of information on the browser, the Cookie methods read(), write() and dispose() are adequate. As their names imply, these read, write and delete cookies from [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://docs12b.mootools.net/Plugins/Hash.Cookie" onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}"><img style="float:right; margin:0 0 10px 10px;cursor:pointer; cursor:hand;" src="http://www.thetruetribe.com/uploaded_images/doobiecookie-722766.jpg" border="0" alt="" /></a></p>
<p>MooTools makes working with cookies quite easy. Based on functions from Paul Peter Koch&#8217;s <a href="http://www.quirksmode.org/">QuirksMode</a>, the MooTools API for handling cookies is intuitive and easy to remember.<span id="more-23"></span></p>
<p>For storing simple bits of information on the browser, the <a href="http://docs12b.mootools.net/Utilities/Cookie">Cookie methods</a> <span style="font-style:italic;">read(), write()</span> and <span style="font-style:italic;">dispose()</span> are adequate. As their names imply, these read, write and delete cookies from the browser, respectively.</p>
<p>But, if you want to do something more complex, such as save a JSON string each time a method is called on the cookie, then <a href="http://docs12b.mootools.net/Plugins/Hash.Cookie">Hash.Cookie</a> is for you.</p>
<p>What&#8217;s the difference between the Hash.Cookie methods and the standard methods you say? For one, Hash.Cookie is its own module, so make sure the version of MooTools you&#8217;re using has it included. Besides that, Hash.Cookie gives you all the functionality of the Hash class, including getters/setters, <span style="font-style:italic;">each(), some(), filter(), has(), keyOf()</span> and more. For the full documentation on all Hash methods available, view the <a href="http://docs12b.mootools.net/Native/Hash/#Hash">Hash docs here</a>.</p>
<p>Here&#8217;s an example that uses the Hash.Cookie class to store <span style="font-weight:bold;">how many times a user has visited</span> the page. Note that this uses Firebug so if you don&#8217;t have it enabled, you&#8217;ll need to override <a href="http://www.thetruetribe.com/2008/04/redefining-consolelog-for-browsers.html">console.log()</a>.</p>
<pre class="js">window.addEvent('domready', function() {
    var cookie = new Hash.Cookie('test-cookie');

    var numberOfVisits = $defined(cookie.get('numberOfVisits')) ?
                         cookie.get('numberOfVisits') :
                         0;

    cookie.set('numberOfVisits', numberOfVisits + 1);

    console.log('You have visited the page ' +
                 cookie.get('numberOfVisits') +
                 ' times.');
});</pre>
<p>On line 1, we start by declaring everything to occur when the DOM is ready.<br />
For MooTools developers, this should be a familiar practice. Although we aren&#8217;t<br />
manipulating the DOM in this example, I&#8217;ve left it in, as Firebug threw an error<br />
when I tried to console.log() before the DOM was ready.</p>
<p>On line 2, we instantiate a new cookie named &#8216;test-cookie&#8217; without any options. Interestingly, this also has the effect of <span style="font-weight:bold;">loading the cookie</span> if it already exists. This means that the first time the user visits the page, it will write a cookie, but on all subsequent visits it will read a cookie. The end result is the same: we have a reference to the cookie as a hash object, and can use the methods that the Hash class provides.</p>
<p>Next, on lines 4-6, we use a <span style="font-weight:bold;">ternary operator</span> to check if &#8216;numberOfVisits&#8217; has already been written to the cookie. If so, we read it from the cookie. If not, it is initialized to 0.</p>
<p>On line 8, we increment &#8216;numberOfVisits&#8217; using the <span style="font-style:italic;">set()</span> method. Finally, we log the number of visits using Firebug&#8217;s <span style="font-style:italic;">console.log()</span>.</p>
<h3>Getters, Setters and Auto-Saving Cookies</h3>
<p>Getters and setters should be familiar to anyone versed in Java but I imagine some JavaScript developers may be unfamiliar with them. Essentially, <span style="font-style:italic;">get()</span> and <span style="font-style:italic;">set()</span> methods are provided for you to use instead of directly accessing the data. For instance, cookie.hash.numberOfVisits and cookie.get(&#8216;numberOfVisits&#8217;) both return the same data, but the former directly accesses the data while the latter uses a wrapper method.</p>
<p>There are numerous reasons to use getters/setters instead of directly accessing the data, but one practical example is the use of Hash.Cookie&#8217;s autoSave functionality. There is an <span style="font-weight:bold;">autoSave flag in the options</span> which, if enabled, will save the data after each operation on the hash. It defaults to &#8216;true&#8217; which is why I didn&#8217;t have to <span style="font-style:italic;">write()</span> the cookie in the above example.</p>
<p>Since I didn&#8217;t define a duration in the options object when instantiating the cookie (nor did I even include an options object, for that matter), it will only live for the duration of the browser. In other words, it&#8217;s a session cookie: When the user exits the browser and restarts, the cookie will be destroyed. For the cookie to live beyond the lifespan of the browser, you can specify the duration of the cookie to live (in days).</p>
<p>The other options which I omitted were <span style="font-style:italic;">domain, path</span> and <span style="font-style:italic;">secure</span>. None of those were important for my example but for some applications, setting domain- and path-specific cookies and ensuring they are only accessed over HTTPS may be necessary</p>
<p>This example is somewhat simple but I hope it has provided an introduction to using MooTools&#8217; excellent Hash.Cookie API. Good luck and enjoy a tall glass of soymilk with your Hash Cookies!</p>
<a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.thetruetribe.com%2F2008%2F05%2Fusing-mootools-hash-cookie-api%2F&amp;linkname=Using%20MooTools%26%238217%3B%20Hash.Cookie%20API"><img src="http://www.thetruetribe.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a>]]></content:encoded>
			<wfw:commentRss>http://www.thetruetribe.com/2008/05/using-mootools-hash-cookie-api/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
