I have below code for the problem but it is not working fine. If a char is lowercase it turns it into uppercase and vice versa. But what i am doing is first making the entire string to lowercase then changing first char as uppercase.
Examples -
str = "I’m a little tea pot"
Output = “i’m A Little Tea Pot”
str = "HERE IS MY HANDLE HERE IS MY SPOUT"
output = “here is my handle here is my spout”
function titleCase(str) {
var wordsArray = str.split(' ');
var newArray = [];
for(var i =0;i<wordsArray.length;i++){
newArray[i] = wordsArray[i].toLowerCase().replace(wordsArray[i].charAt(0), wordsArray[i].charAt(0).toUpperCase());
//console.log(newArray);`
}
str = newArray.join(' ');
console.log(str);
return str;
}
titleCase("I'm a little tea pot");
LInk - https://www.freecodecamp.org/challenges/title-case-a-sentence
I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>
) will also add backticks around text.
OK, this took me a second.
In this code,
newArray[i] = wordsArray[i].toLowerCase().replace(wordsArray[i].charAt(0), wordsArray[i].charAt(0).toUpperCase());
in the replace, you are checking the case in wordsArray. But you don’t know what the case is in wordsArray. In your test string, the first char is I. So it checks and replaces any I in the current string with I. But this string has already converted to lowercase so there is nothing to change.
I’m sure there is a more elegant solution, but I’d simply do this is two steps. Create your newArray[i] with a lowercase version of wordsArray[i] and then do your replace magic, but referring to newArray[i].
Thanks a lot for this valuable information. I was looking for something like we do on stackoverflow so the code gets more readable.
I tried below but it is not working.
function titleCase(str) {
var wordsArray = str.split(' ');
var newArray = [];
for(var i =0;i<wordsArray.length;i++){
newArray[i] = wordsArray[i].toLowerCase();
newArray[i].replace(newArray[i].charAt(0), newArray[i].charAt(0).toUpperCase());
//console.log(newArray);`
}
str = newArray.join(' ');
console.log(newArray);
return str;
}
titleCase("I'm a little tea pot");
replace("l", "L")
will replace every occurence of “l” in string, so word “little” will be converted to “LittLe”. Thus you must get only first char of the word, exactly charAt(0)
, convert it to uppercase, and then append to it rest of the word starting from second char. Try read the String.prototype.substr()
I’m sorry but that is incorrect. In order to do that (AFAIK), you would have to use some kind of regex.
var myStr1 = "little";
var myStr2 = myStr1.replace(myStr1.charAt(0), myStr1.charAt(0).toUpperCase());
console.log("myStr2 =", myStr2)
// myStr2 = Little
var myStr3 = myStr1.replace(/l/g,"L";
console.log("myStr3 =", myStr3)
// myStr3 = LittLe
The issue he was having had nothing to do with his use of replace.
Oh, really!
…Only the first occurrence will be replaced.
I looked at the code with an error again and now see what is a problem - no assignment result of replace. It’s just lost.