Title Case a Sentence...how to continue

Hi,

I wonder how could I change this code to work? I know in theory what to do but don’t know how to make code that works:

  • put the words into substrings

-put them into lowercase

-put the first letter into upper case in every string

-combine the substrings into one string again

function titleCase(str) {

 var sentence = str.split(' ');
  return sentence;
  var tolowercase = sentence.toLowerCase();
  return tolowercase;
 
  


 
}

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

It works only from this part:

function titleCase(str) {

 var sentence = str.split(' ');
  return sentence;
}
titleCase("I'm a little tea pot");

I know there are ready solutions for this task and everything else online but if I just copy them I don’t learn. By the way, what does mean “unreachable var after return”?

I would add that it looks like impossible to use/add other methods inside that titlecase function. Like toLowerCase, ToUpperCase, to Join. I really don’t understand why. Looks like there is “room” omly for one var and return combination.

Once a function reaches a return, it stops running.

In your case your function is getting to:
return sentence;
and then exiting the function.

var sentence = str.split(' ');
is already returning the array created by the split method and storing it in your sentence variable, so you don’t need to have a return here.

You can do all of your string/array manipulation and then have only a single return at the bottom of your function which returns the finished string.

Hope that helps!

2 Likes
function titleCase(str) {

 var sentence = str.toLowerCase().split(" ");
  sentence.charAt(0).toUpperCase();
 return sentence;
}

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

I wonder why it says: “TYpeError: sentence.chartAt is not a function.”

This part works well:

function titleCase(str) {

 var sentence = str.toLowerCase().split(" ");
 return sentence;
}

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

Hello CoderInFreeCodeCamp,

When you call split on a string the result will be an array of strings. To use the charAt function you first have to specify which string in the array you are talking about using the square brackets, e.g. sentence[0].

1 Like

I need to capitalize the first word in every substring. Too hard.

It looks like you’re on the right track using split(’ '). I would recommend looking into the map() function. Also, keep in mind how you would reference the first character of a string. For example the if I had
var str = “hello”;
I could reference the ‘h’ in that string with
str[0];
Also, look into the substr() function.

1 Like

To clarify what @midenfors was getting at:

Your split function leaves you with an array that should look something like:

[“i’m”, “a”, “little”, “tea”, “pot”]

You’ll need to find a way to apply the toUpperCase() function to the first character of each string in your array.

1 Like

Just to beat a dead horse, the reason sentence.charAt(0).toUpperCase(); returns a type error is because you are trying to apply charAt to an array. There is no charAt function for arrays. As stated by others, that array is an array of strings. You must specify to which string you are applying it.

1 Like

I have progressed a little bit. Here is my newest code. I just have to figure out how to capitalize the first word.

function titleCase(str) {
var sentence = str.toLowerCase().split(" ").join(" ");

 return sentence;

}

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

For some reason when I add str[0].toUpperCase() after join it does not work. It says that “str.toLowerCase(…).split(…).join(…).str is undfined”.

But the code which I posted last works. This does not:

function titleCase(str) {
var sentence = str.toLowerCase().split(" ").join(" ").str[0].toUpperCase();

 return sentence;


}



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

Immediately after you split the sentence into an array you’re joining it back together to make a string. Keep in mind that after you split the string you now are working with an array. Once you have an array, you could then call an array method that allows you to do something with each array element. Read up on map and forEach.

1 Like

array prototype for each has for example this example on mozilla’s web page https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
:

var a = ['a', 'b', 'c'];

a.forEach(function(element) {
    console.log(element);
});

// a
// b
// c

Is sentence kinda same as variable a in that example above? Because if I put

function titleCase(str) {
var sentence = str.toLowerCase().split(" ");
console.log(sentence);

it should return a new array? edit: actually this does not work.
one more edit: is str same as element in that example above?

Is sentence kinda same as variable a in that example above?

Not really. The variable ‘a’ from the first snippet is set to an array, then forEach() is called on that array. In the second snippet the variable ‘sentence’ is the result of creating an array from the variable ‘str’ by using the string method split(). You could then go on to do

sentence.forEach(function(element) { console.log(element) });

which would then be similar to the first snippet. Even better, instead of creating an array and storing it in the sentence variable you just do this

return str.toLowerCase().split(" ").forEach( /* do something in here */ )

and the function would return an array.

Feells like I just dont find the solution.

str = titleCase;
function titleCase(str) {
return str.toLowerCase().split(" ").forEach(str[0].toUpperCase()).join(" ");
}

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

It says: str[0]toUpperCase is not a function.

Edit: everything else works in this code.

Inside forEach you should be executing a function. Read up on arrow functions. Inside forEach() you can define a function that will iterate through the array and perform some action on it.

Edit:
You are very close to a solution!

1 Like