{"id":296,"date":"2016-01-11T11:04:00","date_gmt":"2016-01-11T11:04:00","guid":{"rendered":"http:\/\/www.thespidercloud.com\/?p=132"},"modified":"2018-05-29T05:52:47","modified_gmt":"2018-05-29T05:52:47","slug":"study-of-a-function-titlecase","status":"publish","type":"post","link":"https:\/\/frowningbear.com\/codebase\/2016\/01\/11\/study-of-a-function-titlecase\/","title":{"rendered":"Study of a Function &#8211; titleCase()"},"content":{"rendered":"<p>Lets try and do a detailed analysis\/deconstruction of a function <code>titleCase()<\/code> which takes a string and capitalises the first letter of every word. It uses arrays and some string methods and demonstrates usage of the return keyword and the practice of passing one function as an argument to another.<\/p>\n<p><!--more--><br \/>\n[javascript wraplines=&#8221;true&#8221;]<br \/>\n\/\/ the original code<br \/>\nvar testString =&quot;tedwood excellent bear&quot;;<\/p>\n<p>function titleCase(str) {<br \/>\n  var strArray = str.split(&quot; &quot;);<br \/>\n  var i;<br \/>\n  for (i = 0; i &amp;lt; strArray.length; i++){<br \/>\n    strArray[i] = capitalise(strArray[i]);<br \/>\n  }<br \/>\n  return(strArray);<br \/>\n}<\/p>\n<p>function capitalise(str) {<br \/>\n  var result = [];<br \/>\n  result[0] = str.charAt(0).toUpperCase();<br \/>\n  result[1] = str.substring(1);<br \/>\n  return(result.join(&quot;&quot;));<br \/>\n}<\/p>\n<p>console.log(titleCase(testString)); \/\/ Tedwood Excellent Bear<br \/>\n[\/javascript]<\/p>\n<p>The code uses 2 functions defined by the author but for the sake of demonstration and comparison we can rewrite that code to not need any self-defined functions.<\/p>\n<p>[javascript]\/\/ deconstructed with no functions, just sequential code<br \/>\nvar testString =&quot;tedwood excellent bear&quot;;<br \/>\nvar strArray = testString.split(&quot; &quot;);<br \/>\nvar i;<br \/>\nfor (i = 0; i &amp;lt; strArray.length; i++){<br \/>\n  var currentWord = strArray[i];<br \/>\n  var result = [];<br \/>\n  result[0] = currentWord.charAt(0).toUpperCase();<br \/>\n  result[1] = currentWord.substring(1);<br \/>\n  strArray[i] = result.join(&quot;&quot;);<br \/>\n}<br \/>\nconsole.log(strArray); \/\/ Tedwood Excellent Bear<br \/>\n[\/javascript]<br \/>\nNow lets modify it slightly by putting that code into one function only.<br \/>\n[javascript]\/*<br \/>\nThis is a shortened version of<br \/>\nthe original code rewritten to<br \/>\nuse only one function<br \/>\n*\/<\/p>\n<p>var testString =&quot;tedwood excellent bear&quot;;<\/p>\n<p>function titleCase(str) {<br \/>\n  var strArray = str.split(&quot; &quot;);<br \/>\n  var i;<br \/>\n  for (i = 0; i &amp;lt; strArray.length; i++){<br \/>\n    var currentWord = strArray[i];<br \/>\n\tvar result = [];<br \/>\n    result[0] = currentWord.charAt(0).toUpperCase();<br \/>\n\tresult[1] = currentWord.substring(1);<br \/>\n    strArray[i] = result.join(&quot;&quot;);<br \/>\n  }<br \/>\n  return(strArray);<br \/>\n} <\/p>\n<p>console.log(titleCase(testString)); \/\/ Tedwood Excellent Bear<br \/>\n[\/javascript]<br \/>\nLets try a bit of Switcheroo! A minor variation of the previous. Only the last statements have been changed. Can you see the difference?<br \/>\n[javascript]<br \/>\nvar testString =&quot;tedwood excellent bear&quot;;<\/p>\n<p>function titleCase(str) {<br \/>\n  var strArray = str.split(&quot; &quot;);<br \/>\n  var i;<br \/>\n  for (i = 0; i &amp;lt; strArray.length; i++){<br \/>\n    var currentWord = strArray[i];<br \/>\n\tvar result = [];<br \/>\n    result[0] = currentWord.charAt(0).toUpperCase();<br \/>\n\tresult[1] = currentWord.substring(1);<br \/>\n    strArray[i] = result.join(&quot;&quot;);<br \/>\n  }<br \/>\n  console.log(strArray);<br \/>\n}<\/p>\n<p>titleCase(testString); \/\/ Tedwood Excellent Bear<br \/>\n[\/javascript]<br \/>\nFinally here is the original code supplemented with extensive logs to help visualise what&#8217;s going on more clearly.<\/p>\n<p>[javascript]<br \/>\n\/\/ the final version, the original code with all the logs for clarity<br \/>\nvar testString =&quot;tedwood excellent bear&quot;;<\/p>\n<p>function titleCase(str) {<br \/>\n  var strArray = str.split(&quot; &quot;);<br \/>\n  console.log(&quot;String converted to array:&quot;);<br \/>\n  console.log(strArray);<br \/>\n  console.log(&quot; &quot;);<br \/>\n  var i;<br \/>\n  for (i = 0; i &amp;lt; strArray.length; i++){<br \/>\n    console.log(&quot;i is now: &quot; + i);<br \/>\n    console.log(&quot;Current array item is: &quot; + strArray[i]);<br \/>\n    console.log(&quot;Now send to capitalise function&#8230;&quot;);<br \/>\n    strArray[i] = capitalise(strArray[i]);<br \/>\n    console.log(&quot;  Updated array is: &quot;);<br \/>\n    console.log(strArray);<br \/>\n    if(i &amp;lt; strArray.length &#8211; 1) {<br \/>\n      console.log(&quot;  Loop over next item in array&#8230;&quot;);<br \/>\n    }<br \/>\n    console.log(&quot; &quot;);<br \/>\n  }<br \/>\n  console.log(&quot;Final output is: &quot;);<br \/>\n  return(strArray);<br \/>\n}<\/p>\n<p>function capitalise(str) {<br \/>\n  var result = [];<br \/>\n  result[0] = str.charAt(0).toUpperCase();<br \/>\n  console.log(&quot;  Capitalised first letter is: &quot; + result[0]);<br \/>\n  result[1] = str.substring(1);<br \/>\n  console.log(&quot;  Rest of the word is: &quot; + result[1]);<br \/>\n  console.log(&quot;  Joined result is: &quot; + result.join(&quot;&quot;));<br \/>\n  console.log(&quot;  Put capitalised result back into first array&#8230;&quot;);<br \/>\n  return(result.join(&quot;&quot;));<br \/>\n}<\/p>\n<p>console.log(titleCase(testString)); <\/p>\n<p>\/* Running the code in Firebug produces this result<\/p>\n<p>String converted to array:<br \/>\n[&quot;tedwood&quot;, &quot;excellent&quot;, &quot;bear&quot;]<\/p>\n<p>i is now: 0<br \/>\nCurrent array item is: tedwood<br \/>\nNow send to capitalise function&#8230;<br \/>\n  Capitalised first letter is: T<br \/>\n  Rest of the word is: edwood<br \/>\n  Joined result is: Tedwood<br \/>\n  Put capitalised result back into first array&#8230;<br \/>\n  Updated array is:<br \/>\n[&quot;Tedwood&quot;, &quot;excellent&quot;, &quot;bear&quot;]<br \/>\n  Loop over next item in array&#8230;<\/p>\n<p>i is now: 1<br \/>\nCurrent array item is: excellent<br \/>\nNow send to capitalise function&#8230;<br \/>\n  Capitalised first letter is: E<br \/>\n  Rest of the word is: xcellent<br \/>\n  Joined result is: Excellent<br \/>\n  Put capitalised result back into first array&#8230;<br \/>\n  Updated array is:<br \/>\n[&quot;Tedwood&quot;, &quot;Excellent&quot;, &quot;bear&quot;]<br \/>\n  Loop over next item in array&#8230;<\/p>\n<p>i is now: 2<br \/>\nCurrent array item is: bear<br \/>\nNow send to capitalise function&#8230;<br \/>\n  Capitalised first letter is: B<br \/>\n  Rest of the word is: ear<br \/>\n  Joined result is: Bear<br \/>\n  Put capitalised result back into first array&#8230;<br \/>\n  Updated array is:<br \/>\n[&quot;Tedwood&quot;, &quot;Excellent&quot;, &quot;Bear&quot;]<\/p>\n<p>Final output is:<br \/>\n[&quot;Tedwood&quot;, &quot;Excellent&quot;, &quot;Bear&quot;]<\/p>\n<p>*\/<\/p>\n<p>[\/javascript]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Lets try and do a detailed analysis\/deconstruction of a function titleCase() which takes a string and capitalises the first letter of every word. It uses arrays and some string methods and demonstrates usage of the return keyword and the practice of passing one function as an argument to another.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-296","post","type-post","status-publish","format-standard","hentry","category-javascript"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/posts\/296","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/comments?post=296"}],"version-history":[{"count":4,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/posts\/296\/revisions"}],"predecessor-version":[{"id":1209,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/posts\/296\/revisions\/1209"}],"wp:attachment":[{"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/media?parent=296"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/categories?post=296"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/frowningbear.com\/codebase\/wp-json\/wp\/v2\/tags?post=296"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}