Problem with Title Case a Sentence challenge test?

Tell us what’s happening:
The challenge says I failed all the test (except the first) , but the function outputs all the test cases correctly. What’s going on?

Your code so far

//this function is repurposed from an earlier challenge
//it returns the first word from a string
function extractFirstWord(string){ 
if(/\s/.test(string) == false){
  return string;
  let word = "";
  let stringArr = [...string];
  for (let i = 0; string[i] !== " "; i++){
    word += string[i]
  return word;
//this function takes a string and creates an array whose elements are the words in the string
function createArrayOfWords(str) {
str = str.trim();
let wordArr = []
while(str.length > 0)
  let word = extractFirstWord(str);
str = str.slice(word.length+1, str.length);
return wordArr;
//sends an individual word to title case
function sendWordToTitleCase(word){
word = word.toLowerCase();
let wordArr = [...word];
wordArr[0]= wordArr[0].toUpperCase();
word = "";
for (let i = 0; i < wordArr.length; i++){
  word += wordArr[i];
return word;
//sends a string to title case
function titleCase(str) {
let words = createArrayOfWords(str);
for (let i = 0; i < words.length; i++){
  words[i] = sendWordToTitleCase(words[i])
let result = "";
for (let i = 0; i < words.length; i++){
  result += words[i] + " ";
return result;
//each test case is correct, but it fails the test
console.log(titleCase("I'm a little tea pot"));
console.log(titleCase("sHoRt AnD sToUt"))
console.log(titleCase("HERE IS MY HANDLE HERE IS MY SPOUT"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36.

Challenge: Title Case a Sentence

Link to the challenge:


Here’s a hint for you. :slight_smile:

thank you! why was result.trim() not taking care of that?

what was exactly the problem ?

it appeared to give the right output and fail the tests, but was actually including a space on the end.

Because you call the .trim() prototype but don’t do anything with the value.

Changing this line to result = result.trim() did the trick.


if the original sentence doesn’t have a space at the top and at the bottom, why do you use trim() ?

Because this line appends a space to the end of the result.


1 Like