Title Case a Sentence but how

this code is working when i console log and the output is perfect, but when i click on run the tests the code is not working. ANY HELP


function titleCase(str) {
 let split = str.toLowerCase().split(" ")
 let newword = ""
 for(let i=0; i<split.length; i++){
newword += split[i][0].toUpperCase()+split[i].slice(1)+" "

 }
 return newword;
}

titleCase("I'm a little tea pot");

Challenge: Title Case a Sentence

Link to the challenge:

Your output has an extra space at the end.

I suggest storing your words in an array, and then returning the result of the join() method.

so i added these lines
let join = newword.split(' ') return join.join(' ')
instead of return newword
but still not working

With your original code, you could try the trim method here

i do not know how to use trim

Trying looking at the docs and see how you can use it in your return statement here.

what’s your whole code now?

function titleCase(str) {
 let split = str.toLowerCase().split(" ")
 let newword = ""
 for(let i=0; i<split.length; i++){
newword += split[i][0].toUpperCase()+split[i].slice(1)+" "

 }
 let join = newword.split(' ') return join.join(' ')
instead of return newword
}

titleCase("I'm a little tea pot");

Why don’t you create a second array with your loop instead of forcing join in there like that?

this does exactly the same as

It would be helpful to you if you check for which string this function fails, then check if you can fix that. It can be dauting to leave a solution in favour for something new, but it will actually help. Try storing the words in an array could actually be very useful to push each word into an empty array after turning the first character to Uppercase, then use the newword.join(’ ')

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.