Title Casing a sentence logic nit working

Tell us what’s happening:

Tell me if line 2 works why not line 8?

Your code so far

function titleCase(str) {
str = str.toLowerCase();

for(let i = 0; i < str.length; i++){
  if(str[i] == ' '){

return str;

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; rv:78.0) Gecko/20100101 Firefox/78.0.

Challenge: Title Case a Sentence

Link to the challenge:

This line does not work. You cannot mutate a string like this. :slightly_smiling_face:

1 Like

In addition to @nhcarrigan’s answer above, there are better ways of solving this challenge like converting the string to an array of words and looping through instead of looping through an array of characters. And be careful with str[i+1].toUpperCase();. Besides .toUpperCase() returning a new string, you run a risk of calling it with undefined if the string provided ends with " " which will throw an error.