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();
How did you think that the first argument is the string? It is supposed to be this
. Replacing str
with this
and works:
String.prototype.toCapitalize = function () {
let splits = this.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';
console.log(h.toCapitalize());
I have a submit button, now I want result like this: When I click on first time it will be run action 1. When I click on second time it will be run action 2. When I click on n time it will be run ...
I have a submit button, now I want result like this: When I click on first time it will be run action 1. When I click on second time it will be run action 2. When I click on n time it will be run ...
How do I add and remove a class only to selected radio button of a set of radio buttons? <table class="radioClass" role="set1"><tbody><tr><td><input type="radio" value=" ...
How do I add and remove a class only to selected radio button of a set of radio buttons? <table class="radioClass" role="set1"><tbody><tr><td><input type="radio" value=" ...
I am building an offline functionality for a Cordova app that I am developing and I have a php file that gets only the list of images and I have taken the JSON from it and saved in the client device ...
I am building an offline functionality for a Cordova app that I am developing and I have a php file that gets only the list of images and I have taken the JSON from it and saved in the client device ...
I'm working on a project, and we use a lot of coding languages for different parts. I've tried to simplify the problem as much as I can, and came up with the following JSfiddle. The problem is, I ...
I'm working on a project, and we use a lot of coding languages for different parts. I've tried to simplify the problem as much as I can, and came up with the following JSfiddle. The problem is, I ...