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
**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
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
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.
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.
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
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.