Split function & apostrophe

Tell us what’s happening:
Describe your issue in detail here.
I am trying to split the given string into words and I could only think of using the split function for that but I am not sure how that works with the apostrophe. When I used the console.log() to see what myStr is, it displayed as ‘I’m’. How can I make the word include what comes after the apostrophe?

  **Your code so far**

function titleCase(str) {
let myStr = str.split(" ");
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/96.0.4664.110 Safari/537.36

Challenge: Title Case a Sentence

Link to the challenge:

I’m a little confused by the question. Are you trying to split apart “I’m” into “I” and “m”?
What you have right now will split on spaces, so you would get
["I'm", "a", "little", "tea", "pot"]

1 Like

I’m not sure what you are asking?

function titleCase(str) {
  let myStr = str.split(" ");
  console.log(myStr); // [ 'I\'m', 'a', 'little', 'tea', 'pot' ]
  return str;
}

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

The ’ is just another character, just like an m or i.

1 Like

I am trying to split it as “I’m” and with the code below, ‘m’ after the apostrophe disappears and I don’t know why.

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

titleCase(“I’m a little tea pot”);

I don’t think you are correctly understanding what is going wrong, which is leading you to ask for a solution to a problem you aren’t experiencing.

function titleCase(str) {
  let myStr = str.split(" "); // This is not a string, so myStr is a bad name
  console.log("Before loop:");
  console.log(myStr);
  for (let i = 0; i < myStr.length; i++) {
    console.log("Iteration " + i + ":");
    console.log("  start: ", myStr);
    myStr[i] = myStr[i].toLowerCase();
    console.log("  middle: ", myStr);
    myStr[0] = myStr[0].charAt(0).toUpperCase();
    console.log("  final: ", myStr);
  }
  console.log("After loop:");
  console.log(myStr);
  return str;
}

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

I think I see what I was doing wrong. I have some other questions though, is what I create with str.slice(" ") an array?
Also, this is the final version of my code and it seems to return the expected outcome for the tests but I cannot see what is preventing me from passing them? I would appreciate any hint.

function titleCase(str) {

  let myArr = str.split(" ");

  for(let i = 0; i < myArr.length; ++i) {

    myArr[i] = myArr[i].toLowerCase();

    

    if(myArr[i] == "the" || myArr[i] == "of") {

      myArr[i].charAt(0).toUpperCase();

    }
myArr[i] = myArr[i].charAt(0).toUpperCase() + myArr[i].slice(1);

  }

  myArr = myArr.join(" ");

  console.log(myArr);

  return str;

}

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

Yeah. myArr is a better name, but something like strSplitByWord is better yet.


This looks like hard-coding to an answer. You don’t want to go this route. Your solutions should work for any phrases.


This isn’t an array anymore.


Are you sure this is what you want to return?

2 Likes

Yeah, I did not realize that I was still returning str. Also with the hard-coding part, do you mean that it should work for all versions of the and of like tHe, oF etc?
Actually, just checked it and it works for tHe, thE etc.

It shouldn’t matter that myArr[i] is “the” or “of” or anything else. There should be no need for a special case if statement like that.

1 Like

Oh yeah, cause I am capitalizing all words anyways. I guess my mind just tricked me into writing another condition for the and of because of the last part of the instructions. Thank you!

1 Like

It can be easy to end up with over-complicated code when you are trying to work out a solution piece-by-piece. It’s a good practice to check your code for “clutter” that may not help and might even hurt your solution.

Good job on working through this. It looks like you’ve taken the time to learn a lot from this challenge.

2 Likes