-
-
Save anonymous/427f8e5c2e793fe9e1c4 to your computer and use it in GitHub Desktop.
http://www.freecodecamp.com/twhite96 's solution for Bonfire: Title Case a Sentence
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Bonfire: Title Case a Sentence | |
| // Author: @twhite96 | |
| // Challenge: http://www.freecodecamp.com/challenges/bonfire-title-case-a-sentence?solution=function%20titleCase(str)%20%7B%0A%20%20%0A%20%20%2F%2F%20Made%20the%20string%20an%20array%0A%20%20var%20arrayList%20%3D%20str.split(%27%20%27)%3B%0A%20%20%0A%20%20%2F%2F%20iterated%20through%20the%20arrayList%0A%20%20for%20(var%20i%20%3D%200%3B%20i%20%3C%20arrayList.length%3B%20i%2B%2B)%20%7B%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20Set%20placeholders%20for%20each%20element%20in%20the%20array%0A%20%20%20%20var%20eaElement%20%3D%20arrayList%5Bi%5D%3B%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20upperLetter%20placeholder%20to%20uppercase%20each%20array%20element%0A%20%20%20%20var%20upperLetter%20%3D%20eaElement.charAt(0).toUpperCase()%3B%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20Sliced%20at%20first%20letter%2C%20then%20made%20each%20letter%20lowercase%0A%20%20%20%20eaElement%20%3D%20eaElement.slice(1%2CeaElement.length).toLowerCase()%3B%0A%20%20%20%20console.log(eaElement)%3B%0A%20%20%20%20%0A%20%20%20%20%2F%2F%20added%20upperLetter%20and%20each%20element%20together%0A%20%20%20%20arrayList%5Bi%5D%20%3D%20upperLetter.concat(eaElement)%3B%0A%20%20%20%20console.log(arrayList)%3B%0A%20%20%7D%0A%20%20%0A%20%20%2F%2F%20converted%20array%20back%20into%20list%0A%20%20str%20%3D%20arrayList.join(%27%20%27)%3B%0A%20%20%0A%20%20%2F%2F%20returned%20value%20of%20str%0A%20%20return%20str%3B%0A%7D%0A%0AtitleCase(%22I%27m%20a%20little%20tea%20pot%22)%3B%0A | |
| // Learn to Code at Free Code Camp (www.freecodecamp.com) | |
| function titleCase(str) { | |
| // Made the string an array | |
| var arrayList = str.split(' '); | |
| // iterated through the arrayList | |
| for (var i = 0; i < arrayList.length; i++) { | |
| // Set placeholders for each element in the array | |
| var eaElement = arrayList[i]; | |
| // upperLetter placeholder to uppercase each array element | |
| var upperLetter = eaElement.charAt(0).toUpperCase(); | |
| // Sliced at first letter, then made each letter lowercase | |
| eaElement = eaElement.slice(1,eaElement.length).toLowerCase(); | |
| console.log(eaElement); | |
| // added upperLetter and each element together | |
| arrayList[i] = upperLetter.concat(eaElement); | |
| console.log(arrayList); | |
| } | |
| // converted array back into list | |
| str = arrayList.join(' '); | |
| // returned value of str | |
| return str; | |
| } | |
| titleCase("I'm a little tea pot"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment