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(); } }