Code Garage

Test of the garage. Taking a simple category in the blog and filtering out future code snippets, projects and tutorials to share. Hopefully once again encouraging me to post/share what I know or what I am learning at the moment.

Below is my first javascript application attempted a few years ago. A rudimentary calculator to determine the daily, monthly and yearly cost to feed a pet. The perfect code snippet to start the garage with. A nice humbling piece of code to make cringe in retrospect.

function calcKibble() {

for (var i=0; i < document.frm.kibbleSize.length; i++){

if (document.frm.kibbleSize[i].checked){
	var kDensity = document.frm.kibbleSize[i].value;
	
}
	
}
if (document.frm.kibbleSize[3].checked){
		var boxerBag = document.frm.bagWeight.value * 4;
	}else{
		var boxerBag = document.frm.bagWeight.value;
	}

   
   var costInput = document.frm.bagCost.value.replace(/\$/g, ''); //strip dollar sign out of input for error control 
   
  
   var step1 = boxerBag * kDensity; //number of cups in bag
   var step2 = costInput / step1; //cost per cup
   var perDay = document.frm.feedCups.value * step2; // num of cups per day x cost per cup
   var perMonth = perDay * 30;
   var perYear = perDay * 365;
   var costArray = [perDay, perMonth, perYear];
   var term = ["day", "month", "year"];
   
   len = costArray.length; // length of array
  for (var i = 0; i < len; i++){
	   costArray[i] = parseInt(costArray[i] * 100); // Math
       costArray[i] = parseFloat(costArray[i]/100).toFixed(2); // Round and float decimal two points
      
      
       if(newVar != null){ // if newVar exists
newVar = newVar +  "<div class='" + term[i] + "'><span class='one'>The cost per " + term[i] + " is </span><span class='two'>$" + costArray[i].toString() + "</span></div>"; // then append string newVar
	   }else{
		  var newVar = "<div class='" + term[i] + "'><span class='one'>The cost per " + term[i] + " is </span><span class='two'>$" + costArray[i].toString() + "</span></div>"; // otherwise create variable of newVar as converted numeral to string 
	   }
   }
   
          document.getElementById("footer").innerHTML= newVar; // print calculations to user
        
}

function submitEnter(){
          var keycode = window.event.keyCode;
          if(keycode == 13){
	           calcKibble();
	           }
}

Changes

It is once again time for me to change the appearance of the website. It’s hard to find time and spend lots of time in a re-design of a hobby domain, so I just did a basic responsive site to hopefully concentrate on content rather than display for this site. I have always wanted to use my personal site as sort of a diary of coding. However I never get there because I don’t have an “ideal” display to work with. So here we are with a minimalistic approach to design, and time to start documenting what I do.

Kibble Calculator

Just got my hands on the fantastic editor Coda2, and the first thing I did was go to work on the Kibble Calculator. I put up a basic calculator for now until I can spend some more time on the project, which is promised to be a data monster.

The thing that made it possible for me to finish this beginning stage so quickly was the built in iCloud syncing support, making it hassle free to pick up right where I left off when I got home on my laptop. Coda 2 could definitely use some other features, and maybe a bit better Git support… However this is a big jump for me personally coming from dreamweaver. Not that I couldn’t do anything with dreamweaver that I now do with Coda2, but the work flow output is just a thousand times better.