Tried this on my own, but it's not working. Do you see the issue?

I feel maybe it’s the return line that isn’t working, maybe I can’t add toUpperCase to charAt?

Your code so far


function titleCase(str) {
var lowerCase = str.toLowerCase(); // make it all lowercase
var newStr = lowerCase.split(" "); // separate the strings
for (i = 0; i < newStr; i++) { // loop through all separated strings
  return newStr[i].charAt(0).toUpperCase + newStr.slice(1); // return the first letter capitalized with the addition of the the rest of the string
}
}

console.log(titleCase("HERE IS MY HANDLE HERE IS MY SPOUT")); 

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36.

Challenge: Title Case a Sentence

Link to the challenge:

It looks like you have a return inside your loop. This means your loop will only run once.

1 Like

Hmm I keep forgetting return only runs once. Is the rest of the code ok?

Not sure what to replace return with. This ins’t working

function titleCase(str) {
  var lowerCase = str.toLowerCase(); // make it all lowercase
  var newStr = lowerCase.split(" "); // separate the strings
  for (i = 0; i < newStr; i++) { // loop through all separated strings
    newStr[i].charAt(0).toUpperCase + newStr.slice(1); // return the first letter capitalized with the addition of the the rest of the string
  }
  return newStr;
}

console.log(titleCase("HERE IS MY HANDLE HERE IS MY SPOUT"));

Well, this needs to be stored somewhere.

Also, I think you’re meaning to slice new str[i]

Hi again!

Two things.

  1. You should be getting error saying i is not defined.
  1. If you did console.log(newStr)
    You should get this:
    [ 'i\'m', 'a', 'little', 'tea', 'pot' ]

Remember that since you split the string here

That it needs to be converted back to a string for the final result.

1 Like

I don’t know how to store this. Seems to be what I’m missing, but I can’t figure it out. Also the solutions are solving this in a different way, so I can’t really use it as a reference like usual (they’re not using a for loop)

function titleCase(str) {
  var lowerCase = str.toLowerCase(); // make it all lowercase
  var newStr = lowerCase.split(" "); // separate the strings
  for (let i = 0; i < newStr; i++) { // loop through all separated strings
    newStr[i].charAt(0).toUpperCase + newStr[i].slice(1); // return the first letter capitalized with the addition of the the rest of the string
  }
  return newStr.join(" ");
}

console.log(titleCase("HERE IS MY HANDLE HERE IS MY SPOUT"));

You are really close.

I don’t know why I didn’t catch this before.

There is something important missing here

There is something important missing here

For this part

When you think about it you want to add these new words to the new sentence right.

Think of a way of creating a variable, assigning something to it(like an empty data structure) where you can place the new words in it.

Then you can return the new variable with join method converting it back to a string.

Hope that makes sense.

Sorry I can’t figure this out. I tried to add a new variable for finalStr but it’s not working. Can you give me the solution to this now? I’m sure I’ll understand if you do.

I did add newStr.length here

Made it toUpperCase() otherwise I’m clueless

Can I see the updated code? cause you really are close.

function titleCase(str) {
  var lowerCase = str.toLowerCase(); // make it all lowercase
  var newStr = lowerCase.split(" "); // separate the strings
  var finalStr = "";
  for (let i = 0; i < newStr.length; i++) { // loop through all separated strings
    var finalStr = newStr[i].charAt(0).toUpperCase() + newStr[i].slice(1); // return the first letter capitalized with the addition of the the rest of the string
  }
  return finalStr.join(" ");
}

console.log(titleCase("HERE IS MY HANDLE HERE IS MY SPOUT"));

You can’t use join on a string but you can on an array.
That is what i meant by empty data structure

rewite this part

newStr[i].charAt(0).toUpperCase() + newStr[i].slice(1);

so you can add it to an empty array not an empty string.

Make sense?

I can’t figure it out. Giving me a answer now would help me learn. I’m sorry I can’t seem to figure it out

function titleCase(str) {
  var lowerCase = str.toLowerCase(); // make it all lowercase
  var newStr = lowerCase.split(" "); // separate the strings
  var finalStr = [];
  for (let i = 0; i < newStr.length; i++) { // loop through all separated strings
    var finalStr = newStr[i].charAt(0).toUpperCase() + newStr[i].slice(1); // return the first letter capitalized with the addition of the the rest of the string
  }
  return finalStr.join(" ");
}

console.log(titleCase("HERE IS MY HANDLE HERE IS MY SPOUT"));

You know I can’t give you the answer.
But I promise you are crazy close.

How do you add something to the end of an array?

Modify this line so you can add this to the end of the array.

Right now you are assigning.
Oh and get rid of the var in that line. You don’t need to redeclare it.

1 Like

Ok just got it with that advice

   finalStr.push(newStr[i].charAt(0).toUpperCase() + newStr[i].slice(1));

The problem was I couldn’t use join because it was a string so I had to use push?

I was initially going to use concat, would be curious to know if I could’ve done it that way and if so how?

1 Like

yes :grinning:

Hooray!

The concat method for arrays is if you want to merge two or more arrays together. So using push is the right way to go.

I know these are frustrating but your logic is getting much better.
There were just a few syntax errors.

Keep pushing through the algorithms and if you need to take a short break and come back to it later in the day that is fine to.

Yeah thanks, I have gotten better slowly but surely. I just want to make sure I learned the reason I had the last problem. The problem was I couldn’t use join because finalStr was a string not an array?

So that I can learn better to read the docs, where would I be able to find that bit of information next time?

Yeah will definitely take a break now :sweat_smile:

Yes join is an array method.

In this case, there was an error message in the console when you tried to use join on a string.

It probably said something like join is not a function.

If something is not working always check the console first.
You can always google that error message.

But usually if it says something is not a function that usually means you are trying to use a method that does not exist for strings.

I see, didn’t notice that. Thanks