Title Case a Sentence extra space end of array

Title Case a Sentence extra space end of array
0

#1

Almost got it to work. However, it adds an extra space end of the string.

function titleCase(str) {
var splitStr = str.split(" ");
var capArray = [];
for(var i = 0; i < splitStr.length; i++){
capArray += splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1).toLowerCase() + " ";
}
return capArray;
}

titleCase(“I’m a little tea pot”);


#2

Remove + " " after toLowerCase. That should sort it out.


#3

Then you’ll get CamelCase (no spaces between words).

There are a few ways to fix this. You could use the join() array method to create your returned string, or you could return a slice() of capArray from index 0 to length -1.

Also, it won’t affect the running of your code, but it’s worth noting that your variable naming conventions aren’t quite right - splitStr is actually an array (["I'm", "a", "little", "tea", "pot"]), whereas capArray is initialized as an array but by the time it’s returned it’s actually a string ("I'm A Little Tea Pot ").


#4

I kept the spaces string and just added the splice(0,-1) to remove the space after the last word. Thanks again for the best practices variable naming convention.