You chould start by splitting the string into an array of words.
Hint 2
You should make the word lowercase and make the first letter uppercase.
Hint 3
You will need to create a new string with pieces of the updated words.
Solutions
Solution 1 (Click to Show/Hide)
function titleCase(str) {
const newTitle = str.split(" ");
const updatedTitle = [];
for (let st in newTitle) {
updatedTitle[st] = newTitle[st][0].toUpperCase() + newTitle[st].slice(1).toLowerCase();
}
return updatedTitle.join(" ");
}
Code Explanation
Split the string by white spaces, and create a variable to track the updated title. Then we use a loop to turn turn the first character of the word to uppercase and the rest to lowercase. by creating concatenated string composed of the whole word in lowercase with the first character replaced by its uppercase.
function titleCase(str) {
return str
.toLowerCase()
.split(" ")
.map(val => val.replace(val.charAt(0), val.charAt(0).toUpperCase()))
.join(" ");
}
titleCase("I'm a little tea pot");
Code Explanation
We are making entire string lowercase and then converting it into array. Then we are using map function to replace the lowercase character with uppercase. Finally, we are returning the string using join method.
function titleCase(str) {
return str
.toLowerCase()
.replace(/(^|\s)\S/g, L => L.toUpperCase());
}
Code Explanation
The solution works by first lowercasing all the characters in the string and then only uppercasing the first character of each word.
Lowercase the whole string using str.toLowerCase().
Replace every word’ first character to uppercase using .replace.
Search for character at the beginning of each word i.e. matching any character following a space or matching the first character of the whole string, by using the following pattern.
Regex explanation:
Find all non-whitespace characters (\S)
At the beginning of string (^)
Or after any whitespace character (\s)
The g modifier searches for other such word pattern in the whole string and replaces them.
This solution works with national symbols and accented letters as illustrated by following examples international characters: ‘бабушка курит трубку’ // → ‘Бабушка Курит Трубку’ accented characters: ‘località àtilacol’ // → ‘Località Àtilacol’
function titleCase(str) {
var newArray = str.toLowerCase().split(' ');
var result = [];
for(var i=0; i < newArray.length ; i++){
var val = newArray[i];
result.push(val.replace(val[0], val[0].toUpperCase())) ;
}
return result.join(" ");
}
titleCase("My little tea pot");
I really need to dig deeper into regexp. Super useful for this!
My solution isn’t really new, but just split the str into an array and replaced the first letter of each arr[i] with the upper case version:
function titleCase(str) {
var sentenceUp = [];
var words = str.toLowerCase().split(" ");
for (var i = 0; i < words.length; i++) {
sentenceUp.push(words[i].replace(words[i][0], words[i][0].toUpperCase()));
}
return sentenceUp.join(" ");
}
titleCase("Effective Altruism is an philosophy and global movement dedicated to...");
function titleCase(str) {
var words = str.toLowerCase().split(' ');
for(var i = 0; i<words.length;i++) // for(var i in words)
words[i] = firstLetterUp(words[i]);
str = words.join(' ');
return str;
}
function firstLetterUp(str) {
var word = str.split('');
word[0] = word[0].toUpperCase();
str = word.join('');
return str;
}
titleCase("I'm a little tea pot");
I did not use the replace method, but just .toLowerCase all of the char. well here is the solution i came up with.
function titleCase(str) {
var nString=[];
var nArry=[];
nString=str.toLowerCase().split(' ');
for(var i=0;i<nString.length; i++){
nArry.push(nString[i][0].toUpperCase()+nString[i].slice(1));
}
nString=nArry.join(' ');
return nString;
}
titleCase("I'm a little tea pot");