Title Case A Sentence

Hello there!

This is my first forum post so I apologize in advance as I’m sure I’m very unfamiliar with posting on forums (I’m an introvert). I came up with a solution to the Title Case function and it isn’t working, although I don’t understand why. I wanted to ask in the forums why it isn’t working so that I could avoid making the same mistakes in the future. Here is the code.

function titleCase(str) {
  let splitStr = str.split(' ');
  let finishedWord = '';

  for (let i = 0; i < splitStr.length; i++) {
    let space = ' ';
    let firstLetterOfWord = splitStr[i].charAt(0).toUpperCase();
    let restOfWord = splitStr[i].substr(1).toLowerCase();
    finishedWord += firstLetterOfWord;
    finishedWord += restOfWord;
    finishedWord += space;
    
  }
  return finishedWord ;
}

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

upon console.log(), everything checks out but it still says it isn’t passing tests. Does anyone know if I’m supposed to use a specific method to make it pass? Thanks in advance!!!

You have an extra space character at the end of each string that should not be there.

    finishedWord += space;
1 Like

Try

console.log("***"+titleCase("I'm a little tea pot")+"***");
1 Like

Thank you so much, RandellDawson!! I should have known something like that would creep up with the hackneyed solution I came up with, haha! I am going to research other ways to do it that have less issues. Thanks so much for your time!

Hey there, Jeremy!

What exactly are the ‘*’ doing in the console.log. Sorry if it’s a newb question :frowning:

Did you run that line of code and see?

It shows you the space at the end of your returned string

Yup, when I ran it, it placed the *'s in the front and end where the spaces were. I worded what I meant to ask you wrong though, I meant does the * have any special function or is it showing that I have spaces present that need to be removed? I.E. is this the only way to check for extra spaces or could the same be achieved with any random value, as long as I’m console logging it that way?

1 Like

Any value will work. I used “***” because it wasn’t in your string.

Ah thanks so much, Jeremy, especially for the quick answering! I see the value in using the console log like you showed me! I will apply it going forward to double check things like this. It really is easy to mess up values if you don’t check them!

1 Like

console.log is my favorite debugging tool

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.