Requirements:
Make sure that the first letter of each word in the string is capitalized, and the remainder is lowercase.
Here I have written two methods, or one method, and the other is a variant of the method.
The first type:
function titleCase(str) {
var newarr,newarr1=[];
newarr = str . toLowerCase() . split(" ");
for(var i = 0 ; i < newarr . length ; i++){
newarr1 . push(newarr[i][0] . toUpperCase()+newarr[i] . substring(1));
}
return newarr1.join(‘ ‘);
}
titleCase("I‘m a little tea pot");
The first method I think is better understood.
The second (this is based on the first method of the change):
function titleCase(str) {
var newarr,newarr1;
newarr =str.toLowerCase().split(" ");
for(var i=0;i<newarr.length;i++){
newarr[i] = newarr[i][0].toUpperCase()+newarr[i].substring(1,newarr[i].length));
}
newarr1 = newarr.join(" ");
return newarr1;
}
titleCase("I‘m a little tea pot");
The second method reduces the conversion object, and the principle is the same.
The results of both methods are:
I ' m A Little Tea Pot
PS: If there are deficiencies or errors please point out, must be corrected in time.
JavaScript turns the first letter of each word in a string to uppercase and the rest to lowercase