Examples
Write titleCase so that the following cases hold.
- Input
titleCase("hello world")Output"Hello World" - Input
titleCase("JAVASCRIPT is fun")Output"Javascript Is Fun" - Input
titleCase("a")Output"A"
1 more hidden test cases - 4 in total will be graded.
Answer
js
function titleCase(str) {
return str
.toLowerCase()
.split(" ")
.map((w) => (w ? w[0].toUpperCase() + w.slice(1) : w))
.join(" ");
}
titleCase("JAVASCRIPT is fun"); // "Javascript Is Fun"
Lowercasing first makes all-caps input work. Guard the empty w so repeated spaces don't throw.
