Basic Algorithm Scripting - Title Case a Sentence

Tell us what’s happening:
Following is my code but it is not passing the test, I did console.log but seems fine then what is wrong?

Your code so far

function titleCase(str) {
  let wordsSplit = str.split(" ");
  let capitalized = []
  wordsSplit.map((word)=>{
    const capital = `${word[0].toUpperCase()}${word.slice(1).toLowerCase()} ` 
    capitalized.push(capital)
  })
    return capitalized.join('')
}
titleCase("I'm a little tea pot");

Your browser information:

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

Challenge: Basic Algorithm Scripting - Title Case a Sentence

Link to the challenge:

This problem is hard to spot. Try putting '--' + titleCase("I'm a little tea pot") + '--' in your console.log.

1 Like

Now I did the following, but didn’t find anything abnormal

function titleCase(str) {
  let wordsSplit = str.split(" ");
  let capitalized = []
  wordsSplit.map((word)=>{
    const capital = `${word[0].toUpperCase()}${word.slice(1).toLowerCase()} ` 
    capitalized.push(capital)
  })
    return capitalized.join('')
}

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

Im getting ---I'm A Little Tea Pot --- with that. Try highlighting from the end and tell me if you see anything extra that probably should not be there.

Got it but when I remove the ending space, all words are next to each other without any spaces like

---I'mALittleTeaPot---

You are already using an array method in your code that can help you put those spaces back in. Google some of those methods you are using and hopefully the examples should show you one minor change to fix that.

You could also come up with a way to check if you do or do not need a space added yourself.

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