Title Case a Sentence- not working

Tell us what’s happening:
is this a mutation problem? coz something is not returning what it is supposed to.

Your code so far


function titleCase(str) {
  str = str.toLowerCase();
  str = str.split(" ");
  for(let i=0;i<str.length;i++){
    str[i] = str[i].charAt(0).toUpperCase();
  }
str = str.join(' ');
return str;
}

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

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-algorithm-scripting/title-case-a-sentence

Let’s break down your code using the test case:
So, your str looks like this in beginning:

In first step, you convert your str to lowercase.

Now, str looks like :
"i'm a little tea pot"
Then you split the str into an array:

Now, str looks like:

["i'm","a","little","tea","pot"]

After you use the loops you are changing the ithe element of str to its first character in uppercase.
For example:
"i'm" changes to I
"little" changes to L and so on
And str looks like:

["I","A","L","T","P"]

That means you having only first character and rest of the string is gone.

Try solving now.
Try to figure out how you will get full strings instead of first character.

All the best.

gee…thanks! that was the problem!:sweat_smile:

1 Like