Algorithms- Title Case A Sentence


Hello another post here. I’m working on the Title Case a Sentence exercise. I was able to came up to this solution using a for loop. First I created a variable to lowecase the string, then the result of this variable will be store in an another variable separating/splitting the string into array of words. Once I got my array of words, I made the for loop using the latest variable to iterate each array. I think this has worked so far but when I pass the string into the function, the result duplicates the first letter of each array both lower case and upper case. Did I miss something in here just to get the result of the upper case?

function titleCase(str){
var lowerCase = str.toLowerCase();
eachWord = lowerCase.split(" ");
for (i = 0; i < eachWord.length; i++){
eachWord[i] = eachWord[i].charAt(0).toUpperCase()+eachWord[i].split(1)

return eachWord.join(" ");


titleCase (“JavaScript is hard.”);


I’m the final line of your loop, I think you want to choose a different string method for eachWord[i].split(1). Judging by the parameter you provided to split, I would go so far as to guess that you actually meant to type the right method and just made a typo. Chime back in if you need a more specific hint or are stuck. :grinning:


Thank you so much for helping debug. I really mix up slice and split. I understand the concept of each but for some reason I mix these two.
I don’t know but javascript is getting hard to grasp for me. I should keep practicing and practicing.
Again thanks a lot for the observation!


No worries. The other day I spent at least an hour debugging a simple typo (logical OR operator vs bitwise OR … look so similar when you aren’t paying attention!), so these things happen!