Title case a sentence (Help question)

Alright so I am at a point in the challenge where the hint tells me to use charAt() to specify the first letter in the array. I have done that, but the hint also tells me to embed a function within the replace method as the second argument of replace. This function is supposed to return the capitalized words. I’m getting pretty confused, some help please? Thanks!


let newStr=0;
let newStr1=0
let newStr2=0
function titleCase(str) {
 newStr=str.split("")
   newStr1=newStr.map(v=>v.toLowerCase())
     newStr2=newStr1.map(b=>b.replace(b.charAt(0)))
return newStr2
}

console.log(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/84.0.4147.135 Safari/537.36.

Challenge: Title Case a Sentence

Link to the challenge:

OK, first of all, let’s organize your code a little with “proper” indenting so it’s easier to read:

let newStr=0;
let newStr1=0
let newStr2=0
function titleCase(str) {
  newStr=str.split("")
  newStr1=newStr.map(v=>v.toLowerCase())
  newStr2=newStr1.map(b=>b.replace(b.charAt(0)))
  return newStr2
}

Ok, right off the bat I see a few problems. First of all, I wouldn’t use variable outside the scope of the function (the first three lines) because that can screw up the tests. Actually in this case it might not, but it’s a questionable coding practice and should be avoided. And there’s no need for them to be - they can be const inside the function. (And someone might point out that you don’t need that many variables for this, but let’s not worry about that for now.)

The next thing I would want to do is find out what these things are doing. I would want to log out what those methods are doing:

let newStr=0;
let newStr1=0
let newStr2=0
function titleCase(str) {
  newStr=str.split("")
  console.log('newStr', newStr);
  newStr1=newStr.map(v=>v.toLowerCase())
  console.log('newStr1', newStr1);
  newStr2=newStr1.map(b=>b.replace(b.charAt(0)))
  console.log('newStr2', newStr2);
  
  return newStr2
}

Doing that, I find that str is an array of all the characters.

"newStr is" ["I", "'", "m", " ", "a", " ", "l", "i", "t", "t", "l", "e", " ", "t", "e", "a", " ", "p", "o", "t"]

Is that what you want? Or do you want it broken up some other way? How do you get split to break it up on something other than every character.

The first map is doing what you expect.

The second map is not. replace takes two parameters. And depending on how you handle the split problem, this might not be how you want to do this.

Lastly, you are returning an array, but you’re supposed to be returning a sting. How do you get those individual strings in the array to join together into one string?

There is more than one way to solve this problem, let’s see what you can come up with.

2 Likes

It’s been a long time Kevin! Hope you are doing well. I hope I don’t bother you too much with what might just turn out to be a spitfire of questions lol. Here is what I have so far as per your advice. I wanted to use the .join(" ") after I figured out how to get the first words capitalized. But I added it now so it’s one less step for me later. Here is what I have got so far:

function titleCase(str) {
let newStr=0;
let newStr1=0
let newStr2=0
  newStr=str.split(" ")
    newStr1=newStr.map(v=>v.toLowerCase())
      newStr2=newStr1.map(b=>b.replace(b.charAt(0),b.toUpperCase()))
 return newStr2.join(" ")
}

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

It just shows I'M'm A LITTLEittle TEAea POTot , so far.

Hello~!

Take a look at your replace function here. What are you selecting, and what are you replacing it with? :slight_smile:

1 Like

I’m selecting the first letter so charAt(0) and making it uppercase with b.toUpperCase()? I guess the problem is in this line now

But what is the value of b?

What does b.toUpperCase() return?

1 Like

The value of b is newStr1 I guess? And b.toUpperCase() just returns i'm a little tea pot

Let’s back up a step and look at the whole line.

b is defined as the parameter for your map callback, meaning it will be each element of the array newStr1. You access the first character of the b value with b.charAt(0), then replace that first character with the value of b.toUpperCase().

1 Like

I got it! So I was just adding a copy of the first element to the first element lol. Thanks for the help, so you have to specify the location twice in the replacement statement basically? I though the first argument in the replace was doing that for me anyway, so I just specified it again the the second replacement argument and it passed. I appreciate the help! :grinning:

Here is the passed code:

function titleCase(str) {
let newStr=0;
let newStr1=0
let newStr2=0
  newStr=str.split(" ")
    newStr1=newStr.map(v=>v.toLowerCase())
      newStr2=newStr1.map(b=>b.replace(b.charAt(0),b.charAt(0).toUpperCase()))
 return newStr2.join(" ")
}

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

no, the first argument is what you want to substitute, the second argument is what you want to substitute it with

replace is pretty powerful when used with a regular expression, and a function as second argument

you could really change anything into anything

1 Like