Skip to content

Instantly share code, notes, and snippets.

@AndriiBozh
Created January 8, 2018 23:54
Show Gist options
  • Save AndriiBozh/c22053c971ca857cb33b6c3dc86d2829 to your computer and use it in GitHub Desktop.
Save AndriiBozh/c22053c971ca857cb33b6c3dc86d2829 to your computer and use it in GitHub Desktop.
To Upper Case (freeCodeCamp challenge)
/*
Return the provided string with the first letter of each word capitalized.
Make sure the rest of the word is in lower case.
*/
function titleCase(str) {
function toUpper (word){ //https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace
return word.toUpperCase(); //"The important thing here is that additional operations are needed on the matched item before
} // it is given back as a replacement. The replacement function accepts the matched snippet as its parameter, and uses it to transform the case"
return str.toLowerCase().replace(/(^|\s)\S/g, toUpper); //lowercase the string, then make a replacement using .replace method.
} //here its parameters are regexp and function. RegExp pattern here matches
titleCase("I'm a litTle tea pot"); // \S - a single character other than white space, at the beginning of a string ^ or |
// after a single white space character \s
// g is a flag (regExp parameter). Means global match; tells to find all matches rather than stopping after the first match
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment