I have created a function that changes a phrase to title case:
function toTitleCase(str) {
let arr = str.split(' ');
let x = arr.map(word => {
return word.charAt(0).toUpperCase() + word.slice(1);
});
return x.join(' ');
}
console.log(toTitleCase("A man, a plan, a canal, Panama!"));
I wish I could make this function work like the native toLowerCase()
, by changing it to the string, not passing the string as a parameter:
console.log(("A man, a plan, a canal, Panama!").toTitleCase());
How can I achieve that?