Sequence NODE_154
EasyConvert String to Title Case
JavaScript
Technical Specification
Convert a sentence so that the first letter of each word is uppercase and the rest are lowercase.
Input/Output Samples
Input:"hello world from codewithhappy"
Output:"Hello World From Codewithhappy"
Optimal Logic Path
function toTitleCase(str) {
return str
.split(" ")
.map((word) =>
word ? word[0].toUpperCase() + word.slice(1).toLowerCase() : ""
)
.join(" ");
}Architectural Deep-Dive
We work word-by-word, making the first character uppercase and the rest lowercase.