I created my own method which basically capitalizes the first alphabet of every word in a string.
However, I am getting this Uncaught TypeError: Cannot read property 'split' of undefined error. Where am I wrong?
String.prototype.toCapitalize = (str) => {
let splits = str.split(" ");
let capitalize = '';
splits.forEach((el) => {
let result = el.charAt(0).toUpperCase() + el.substr(1, el.length).toLowerCase();
capitalize = capitalize + ' ' + result;
});
return capitalize;
}
let h = 'its a beautiful weather';
h.toCapitalize();