Title Case a Sentence - I give up. Please help

Been Going at this one awhile… My logic, after splitting the sentence and lower-casing it, was to isolate the first letters, capitalize them, then go get the rest of the word and put the sentence back together… I finally got all the proper letters capitalized, and added on the rest of the words… But there is a space between every. single. character. This is like a bad joke!! So close!!! oof…

console.log(str) gives me — “I ’ m A L i t t l e T e a P o t”

Gonna have to come at this again tomorrw, but any insight advice would be much appreciated!!!

  **Your code so far**

function titleCase(str) {
str = str.toLowerCase().split(" ");
var firstLetters = [];



for(let i = 0; i < str.length; i++) {
  firstLetters.push(str[i][0].toUpperCase())
  for(let j = 1; j < str[i].length; j++)
  firstLetters.push(str[i][j]);
}
str = firstLetters.join(' ')



console.log(str)
return str;
}

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/89.0.4389.114 Safari/537.36.

Challenge: Title Case a Sentence

Link to the challenge:

Sleepy over here too, used mozilla page looked up map method used the built in map method on it, its amazing what a good night sleep will do, your brain will keep working on it all night!

try . join yet?

1 Like

@tannerpace agree 100% - I’ll give map method a look tomorrow - thank you sir!

If you conditionally push a space character at the end of your nested for loop and have your join method using '' it’ll pass. The condition should be such that it does push a space on the last word.

1 Like

Any thoughts why this is still failing?? Console log seems to show correct output

type or paste cfunction titleCase(str) {
  str = str.toLowerCase().split(" ");
  var fixed = [];

  for(let i = 0; i < str.length; i++) {
    fixed.push(str[i][0].toUpperCase());
    for(let j = 1; j <= str[i].length; j++) {
      if(j === str[i].length) {
        fixed.push(" ");
      }
      fixed.push(str[i][j]);
    }
  }

  str = fixed.join("")
  
  
  console.log(str)
  return str;
}

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

Try this

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

Do you see something funny at the end of your output string?

Ah! I sure do!!! Gotta pop that space off at the end… Thank you!!!

1 Like

Finally got it to pass!! Here’s my solution, probably not the most elegant but it works…

function titleCase(str) {
  str = str.toLowerCase().split(" ");
  var fixed = [];
  for(let i = 0; i < str.length; i++) {
    fixed.push(str[i][0].toUpperCase());
    for(let j = 1; j <= str[i].length; j++) {
      if(j === str[i].length && i < str.length - 1) {
        fixed.push(" ");
      }
      fixed.push(str[i][j]); 
    }
  }
  str = fixed.join("");
  return str;
}
titleCase("I'm a little tea pot");```

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