Last active
May 25, 2017 16:37
-
-
Save rodpoblete/951e8d9554a113c45c1ad38aee7cd363 to your computer and use it in GitHub Desktop.
Ttile Case Sentence - Convertir primera letra de cada palabra en mayuscula - FCC [246]
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
function titleCase(str) { | |
str = str.toLowerCase().split(' '); // Convierto el string en minisculas y luego en array con la función split, y le doy el separador (' '). | |
for (var i = 0; i < str.length; i++) { // For para recorrer el array | |
str[i] = str[i].charAt(0).toUpperCase() + str[i].slice(1); // al indice del array convierto en mayuscula el primer caracter con la funcion charAt().toUpperCase() y despues con la funcion slice(1), extraigo el resto del texto y los sumo en una única cadena. | |
} | |
return str.join(' '); // Devuelvo la cadena unida separada por un espacio. | |
} | |
titleCase("I'm a little tea pot"); | |
/* str.length = 5 | |
1st iteration: str[0] = str[0].charAt(0).toUpperCase() + str[0].slice(1); | |
str[0] = "i'm".charAt(0).toUpperCase() + "i'm".slice(1); | |
str[0] = "I" + "'m"; | |
str[0] = "I'm"; | |
2nd iteration: str[1] = str[1].charAt(0).toUpperCase() + str[1].slice(1); | |
str[1] = "a".charAt(0).toUpperCase() + "a".slice(1); | |
str[1] = "A" + ""; | |
str[1] = "A"; | |
3rd iteration: str[2] = str[2].charAt(0).toUpperCase() + str[2].slice(1); | |
str[2] = "little".charAt(0).toUpperCase() + "little".slice(1); | |
str[2] = "L" + "ittle"; | |
str[2] = "Little"; | |
4th iteration: str[3] = str[3].charAt(0).toUpperCase() + str[3].slice(1); | |
str[3] = "tea".charAt(0).toUpperCase() + "tea".slice(1); | |
str[3] = "T" + "ea"; | |
str[3] = "Tea"; | |
5th iteration: str[4] = str[4].charAt(0).toUpperCase() + str[4].slice(1); | |
str[4] = "pot".charAt(0).toUpperCase() + "pot".slice(1); | |
str[4] = "P" + "ot"; | |
str[4] = "Pot"; | |
End of the FOR Loop*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment