Title Case a Sentence lesson

hey guys, so my plan for this lesson, was to loop through the words until I reach the chars, capitalize them and replace. I googled some methods for this one, and this so far what I have, any tips for me? ty.

I tried to search for “format document” for a clearer code before posting but cant seem to find… sry in advance :frowning:

  **Your code so far**

function titleCase(str) {
let words = str.split(' ')
let captChar = ''
let newWords = ''
for (let i =0; i < words.length; i++) {
for (let j =0; j < words[i].length; j++) {
captChar = words[i][0].toUpperCase()

newWords.replace(words[i][0], captChar);
console.log(newWords)
}

}
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/97.0.4692.99 Safari/537.36

Challenge: Title Case a Sentence

Link to the challenge:

Formatted. It really helps if you do this as you write the code.

2 Likes

Maybe it’s OS dependent.


As for your problem, first let’s break it down into a simpler one first.

Instead of a whole sentence (many words separated by spaces), imagine you only have to deal with a single word. How would you write that function?

function toTitleCase(word) {

}

toTitleCase('dog') // Should be 'Dog'

If I were writing this, I would build up a new string to return at the end of the function. It would probably involve concatenating the first letter in upper case, to the rest of the letters in lower case.

function toTitleCase(word) {
  let newWord = word.split('')
  let char = ''
  let anotherWord = word
  for (let i = 0; i < newWord.length; i++) {
    char = newWord[0].toUpperCase();
    let word1 = word.slice(1);
    console.log(char + word1)
  }
}

toTitleCase('dog') // Should be 'Dog'

that’s what I’ve got.
it feels much harder with rest of the words

Can you explain why you’re splitting the word into characters and looping over them? I don’t really see the point.


newWord[0].toUpperCase()

This results in a string that’s the first letter of newWord made upper case.

word.slice(1)

This results in a string that’s all the characters of word starting from the second character.

All you need to do is put those two strings together. How do you concatenate strings?

console.log(char + word1) have it here just instead of console.log I can return.

how else can I access that first letter without looping.
Okay I see I can just access the word after split without the need of looping.

That’s what I’m saying, you already did that right here: newWord[0]

I have a question for you.
This is one word which I know the positions, but if it’s a sentence with multiple words, the positions are unknown that’s the main issue I think.

By the way, you don’t need to split either. You can access characters of strings the same as elements of arrays.

const name = "Daniel"
console.log(name[1]) // "a"

Okay that is nice, one word seems simple, but problem is when you have multiple words, I’ll see what I can do with the new info.

Right, so now you know how to do it with one word. But now we need to deal with multiple words. In your original post you already had a start.

function titleCase(str) {
  const words = str.split(" ");
  for (let i = 0; i < words.length; i++) {
    // But inside the loop you only have to deal with one word.
    // You know how to do that.
    const word = words[i];
  }
}

You’re going to need a new list to keep track of the new words after you make changes to them.

can you place words[i][0] inside a slice function?

Slice only takes indexes (numbers) as arguments. You could use slice to just get the first character from a string "foo".slice(0, 1) would be "f".

function titleCase(str) {
  const words = str.split(" ");
  let newStr = ''
  for (let i = 0; i < words.length; i++) {
    let char = words[i][0].toUpperCase();
    newStr = words[i].replace(words[i][0], char);
  }
console.log(newStr)
}
titleCase("I'm a little tea pot");

that is what I’ve got, I feel like I am close but if I console.log(newStr) after the loop it only logs “Pot”, inside the loop I have the string in a vertical order in the log I guess that’s why it’s not passing.

edit: my issue because it’s an array, if I turn it back to the string, the letters are again lower cased :sweat_smile:

function titleCase(str) {
  const words = str.split(" ");
  let newStr = ''
  for (let i = 0; i < words.length; i++) {
    let char = words[i][0].toUpperCase();
    newStr = words[i].replace(words[i][0], char);
    newStr = words.join()
    console.log(newStr);
  }

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

newStr = whatever ← this overwrites the value, and since it’s inside the loop, you’re only ever gonna get the final word. Remember you can add strings together (concatenate) with the + operator.

See how the coloring is funny for char? That’s the editor telling you that is a bad variable name because it has another meaning.

1 Like
function titleCase(str) {
  str = str.toLowerCase();
  const words = str.split(" ")
  let conString = ''

  for (let i = 0; i < words.length; i++) {
    let captChar = words[i][0].toUpperCase();
    const notAWord = words[i].slice(1)
    conString += captChar + notAWord + " "
    console.log(conString)
  }
  return conString
}

titleCase("I'm a little tea pot");
function titleCase(str) {
  str = str.toLowerCase()
  const words = str.split(" ");
  let newStr = ''
  for (let i = 0; i < words.length; i++) {
    let captChar = words[i][0].toUpperCase();
    newStr += words[i].replace(words[i][0], captChar) + " "
    console.log(newStr);
  }
  return newStr
}
titleCase("I'm a little tea pot");

That’s what I’ve got, how come it is not passing?
both of them the same one with replace one without not passing ;(

What happens here for the very last word?

Unnecessary space?

(20words filler)

Yup. You end up with an extra space at the end of the final string.