Title case a sentence (uppercase in the first letter of the word)
- Requirements
- Make sure that the first letter of each word in the string is capitalized, and the remainder is lowercase.
- Connectors like ' the ' and ' of ' are the same.
- Ideas
- Use. Split ("") to separate sentences into an array of words.
- Use the For loop to separate each word in the array with. Split (") into an array of letters, with the first element in the array capitalized, i.e. the first letter capitalized. Join (") to compose the word
- Finally, each array word is combined with a. Join (') to compose the sentence
- Code
1 functiontitlecase (str) {2 //please write down your code here.3 varTemp1 = Str.tolowercase (). Split ("");4 for(varI =0;i<temp1.length;i++){5Temp1[i] = temp1[i].split (' ');6temp1[i][0]= temp1[i][0].touppercase ();7Temp1[i] = temp1[i].join (' ');8 }9str = temp1.join (' ');Ten returnstr; One } A -Titlecase ("I ' m a Little tea pot");
- RELATED LINKS
- Https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/split
Title Case a Sentence-freecodecamp algorithm topic