# Tag Archives: tutorial

Here’s an example of how to use recover() in Google Go.

```package main

import "fmt"

func gandalf( doTalkTo bool ){
defer catch()
if(doTalkTo){
// Panic stops execution of the current function and stops unwinding the stack, calling an deferred functions along the way.
panic("You shall not pass!")
}
}
// catch() must be called as a deferred
func catch(){
// recover() regains control over the program execution when panic() is called.
// recover() returns is the argument passed from panic()
if r := recover(); r != nil {
fmt.Println("Error:", r)
} else {
fmt.Println("No problems occurred")
}
}
func main() {
gandalf(true)
gandalf(false)
//unwinds to the top of the stack and ends the program
panic("Where am I going?");
}
```

``` Output: Error: You shall not pass! No problems occurred panic: Where am I going? goroutine 1 [running]: main.main() Demo: http://play.golang.org/p/xu9Jd1YI0T More information here: Effective Go - The Program Language Larry BattleI love to program, and discover new tech. Check out my stackoverflow and github accounts.More Posts - Website Follow Me: ```
``` ```
``` How to convert a repeating decimal to a fraction Posted by Larry Battle on July 14, 2012 No comments Here are 4 simple steps to convert a repeating decimal to a fraction. Step 1: Check to see if the number has a repeating decimal. Stop if it doesn’t and do normal conversion. Step 2: Split the decimal into 3 parts; i, x, r. Such that the decimal equals `i.x(r)*`. Step 3: Create a fraction in the form `a/b`. a = ((ixr as int) – (ix as int)) b = ((10^x.length)*(10^r.length – 1)) Step 4: (optional) Reduce the fraction by dividing by the greatest common denominator. Example: 4/3 = 1.333… which sets i = “1”, x = “”, r = “3”. a = (13 – 1) = 12, b = ((10^0)*(10^1 – 1)) = 9 a/b = 12 / 9 = 4/3 Ratio.js does this for you when the `reduce` function is called. Example using Ratio.js: ```Ratio.parse( "1/3" ).reduce().toString() === "1/3"; Ratio.parse( 4/3 ).reduce().toString() === "4/3"; Ratio.parse( 0.123451234512345 ).reduce().toString() === "4115/33333"; Ratio.parse( 0.987987989798979897 ).reduce().toString() === "978108109901/990000000000";``` DEMO Larry BattleI love to program, and discover new tech. Check out my stackoverflow and github accounts.More Posts - Website Follow Me: doT.js Tutorial part 1 Posted by Larry Battle on July 3, 2012 14 comments doT.js Tutorial: Part 1 of 2 What’s dot.js? It’s one of the fastest and concise javascript template engine for nodejs and web browsers, created by Laura Doktorova, the President & Co-Founder of Bebedo Inc. Check out dot.js on Github.com. dot.js works on both the client and server side. But for this tutorial, we’re only going to be focusing on the client-side. Why use a template engine? There are many reasons to use a template engine rather than printing your code in a given language. I think overall, the main reasons for this are code consistency and organization. If you have a good separation of your markup from your logic it becomes easier to debug your code, to share your code with collaborators, and allows for more rapid development by streamlining common methods for outputting your markup. By: Bryce Hamrick How to import doT.js Just include “doT.js” using the script tag. Once included, “doT” will be added to the global namespace. `<script src="https://raw.github.com/olado/doT/master/doT.js"></script>` Steps for using doT.js Create an object literal to store your data. A JSON format is preferred. `var data = { name: "David" };` Create a template that creates the overall structure and tells where your data will be placed. `var template = "Hey <b>{{=it.name}}</b>, you own me money.";` Generate a template function from the template using doT.template(). `var templateFunction = doT.template( template );` Generate HTML by providing data to the template function. `var html = templateFunction( data );` Full source: ```var data = { name: "David" }; // step 1 var template = "Hey <b>{{=it.name}}</b>, you own me money."; // step 2 var templateFunction = doT.template( template ); // step 3 var html = templateFunction( data ); // step 4   // result html == "Hey &lt;b>David&lt;/b>, you own me money.";``` Let’s now examine the function doT.template(). `doT.template(template, settings, userDefinedDef);` Forming a template The content of a template will remain the same, except for the sections that contain javascript encapulated sections (JES). JES look like this. "{{ }}". Note that the data passed to the template function can be reference by the template using the namespace ‘it’. Example: ```// input var data = { thatThingOverThere: "Bartman" }; var template = "Hey, look over there! It's {{=it.thatThingOverThere}}!"; var tempFunc = doT.template(template); var html = tempFunc(data);   // output html == "Hey, look over there! It's Bartman!";``` To better understand what’s going on, let’s look at the generated source for the tempFunc. ```// source for tempFunc function anonymous(it) { var out = 'Hey, look over there! It\'s '+(it.thatThingOverThere)+'!'; return out; }``` Type of JES There two basic type of JES. Type 1 requires a returned value. `var template = "{{=it.value}}";` Type 2 allows for javascript statements to be executed. `var template = "{{ var global = {}; }}";` Example: ```var data = { team: "POWER RANGERS!"} var template = '{{ var chant = "Go go"; }}{{= chant + " " + it.team}}'; var tempFunc = doT.template( template ); var html = tempFunc( data );   // result html == "Go go POWER RANGERS!";   // source for tempFunc function anonymous(it) { var out=''; var chant = "Go go"; out+=( chant + " " + it.team); return out; }``` End of part 1. Part 2 coming soon. Larry BattleI love to program, and discover new tech. Check out my stackoverflow and github accounts.More Posts - Website Follow Me: Code of the day: jQuery, Get comments from HTML elements. Posted by Larry Battle on June 11, 2012 No comments Code of the day: Get comments from HTML elements. Here’s a simple jquery plugin to get the comments from a HTML elements. ```// source code for \$.fn.getComments() \$(function () { /** * \$.fn.getComments() is used to extract the html comments from a HTML elements. * * @author Larry Battle <http://bateru.com/news/contact-me> * @license MIT * @date June 11, 2012 * @version 0.1 * @args {boolean} asArray - If true, returns an array of the comments values. Otherwise returns jquery objects of the node comments. * @example HTML: <div id="example">I am a div. <!--Duh!--></div>   Javascript: \$("#example").getComments(true) // returns [ "Duh!" ] */ var getCommentsFromEl = function (el, asArray) { var result, \$el = \$(el).contents(); result = \$el.filter(function () { return this.nodeType == 8; }); if (asArray) { result = \$.makeArray(result.map(function () { return this.nodeValue; })); } return result; }; \$.fn.getComments = function (asArray) { return getCommentsFromEl(this, asArray); }; });``` Demo and testcases here: http://jsfiddle.net/96rux/ Fork this on Github.com https://github.com/LarryBattle/jQuery.getComments Larry BattleI love to program, and discover new tech. Check out my stackoverflow and github accounts.More Posts - Website Follow Me: Next page »```
``` RSS Feed Game Launch Destruction* jQuery("#startGame").one( "click", function(){ var script = document.createElement( 'script' ); script.type = 'text/javascript'; script.src = "http://hi.kickassapp.com/kickass.js"; jQuery(document.body).append( script ); }); Pages About Me Contact Me Projects Recent CommentsAgung Prakasya on Code of the day: Javascript convert bytes to KB, MB, GB, etcsivanesan M on Answers to “15 Must Know Java Interview Questions After 2 Years of Experience”Andre on Code of the Day: Groovy Print all Methods of an objectProfessor Fontanez on Answers to “15 Must Know Java Interview Questions After 2 Years of Experience”Alex on Simple fix for MSI Windows Installer ErrorStackoverflow Account June 2017 S M T W T F S « Jan      123 45678910 11121314151617 18192021222324 252627282930   Look at past post! June 2017 (3) January 2017 (2) July 2016 (1) January 2015 (1) November 2014 (3) July 2014 (1) May 2014 (1) January 2014 (1) December 2013 (1) November 2013 (2) October 2013 (8) May 2013 (2) March 2013 (5) February 2013 (1) November 2012 (1) October 2012 (2) July 2012 (3) June 2012 (1) May 2012 (6) April 2012 (3) March 2012 (16) February 2012 (2) January 2012 (5) December 2011 (10) November 2011 (7) October 2011 (3) September 2011 (2) August 2011 (4) July 2011 (1) June 2011 (1) May 2011 (12) April 2011 (7) March 2011 (16) January 2011 (2) October 2010 (3) June 2010 (1) March 2010 (1) January 2010 (2) December 2009 (1) November 2009 (1) October 2009 (5) July 2009 (2) April 2009 (2) ```
``` Copyright © 2017 Learning is bateru | Powered by zBench and WordPress ↑ Top //////// Handles toggling the navigation menu for small screens ( function() { var nav = document.getElementById( 'menus' ), button = document.getElementById( 'menus-m' ), menu = document.getElementById( 'menus-dt' ); if ( ! nav ) { return; } if ( ! button ) { return; } // Hide button if menu is missing or empty. if ( ! menu || ! menu.childNodes.length ) { button.style.display = 'none'; return; } button.onclick = function() { if ( -1 !== button.className.indexOf( 'b-toggled-on' ) ) { button.className = button.className.replace( ' b-toggled-on', '' ); menu.className = menu.className.replace( ' toggled-on', '' ); } else { button.className += ' b-toggled-on'; menu.className += ' toggled-on'; } }; } )(); /* <![CDATA[ */ var countVars = {"disqusShortname":"bateru"}; /* ]]> */ ```