Hello,
I’ve just completed the “Title Case a Sentence” algorithm challenge, and although I found it reasonably easy in terms of logic, I’m wondering if there is a cleaner way to approach the solution compared to the code that I have written. My solution was:
function titleCase(str) {
// return an array containing all words in str, converted to lower case
var map1 = str.split(" ").map(function(val) {
return val.toLowerCase();
});
// return an array containing all words in map1, with the first letter capitalized
var map2 = map1.map(function(val) {
var firstUpper = val[0].toUpperCase();
var valRemain = val.slice(1);
return firstUpper.concat(valRemain);
});
// return map2 as a string
return map2.join(" ");
}
titleCase("I'm a little tea pot");
Is there a cleaner way of doing this? Or is my solution ok in terms of code best practice/readability?
Any feedback is very welcome.
Thanks!