Title Case a Sentence - I think my logic is not right

Title Case a Sentence - I think my logic is not right
0.0 0


I’m getting an error in the console saying that arrStr[i].join() is not a function. Am I missing something here?

Thanks in advance!

function titleCase(str) {
// This function will take the string, split it down to words to store in the array, then each word is its own array with the letters in each element. Then the function will take the first element of each word array and convert the letter to uppercase. When done, the array will convert the new words back into a string and return the string as output.

var arrStr = str.split(" "); //split string into words
for(var i=0;i<arrStr.length; i++){ //split words in array into letters
arrStr[i][0].toUpperCase(); //Convert first letter of word to uppercase.
arrStr[i]=arrStr[i].join();//join the letters together to make the words again
//Now that the loop is complete, join the words together to make the sentence, and store as the new str.
str = arrStr.join();
return str;

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


You need to store the .splitted string into a new variable (or in the same variable). In this code the result just goes nowhere.

String functions (like .toUpperCase) don’t modify strings in-place; you need to store in a variable.

arrStr[i].join() fails because at this point you’re arrStr[i] is a string!