/*---------------------------------------------BASIC-------------------------------------------------------*/
function titleCase(str) {
var strPrepare = str.toLowerCase().split(" "); /*готовим стринг к апперкейсу, занижая все под ловеркейс.
Сначала переводим в ловеркейс, потому, что сплит превращает стринг в еррей,а ловеркейс на ерреях не работает*/
var result = strPrepare.map(function(val){
return val.replace(val.charAt(0), val.charAt(0).toUpperCase());//меппим, заменяем первую букву на нее же в апперкейсе
},0);
return result.join(" ");
}
titleCase("I'm a little tea pot");
/*---------------------------------------Advanced Code Solution-------------------------------------------*/
function titleCase(str) {
return str.toLowerCase().replace(/(^|\s)\S/g, (L) => L.toUpperCase());
}
:rocket: Run Code299
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’s 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.