I'm learning Javascript here. My task as of right now is to take a given string and capitalize the beginning of each word, and IF the rest of the word is already caps, put it into lowercase.
What I have so far:
function titleCase(str){
var arrayify = str.split('');
arrayify.forEach(function(current, index){
for (var x = 0; x < current; x++){
var caps = current[x];
return caps;
}
});
}
My test strings:
I'm a little tea pot
sHoRt AnD sToUt
HERE IS MY HANDLE HERE IS MY SPOUT
What I've tried:
function titleCase(str){
str.split(' ').map(s => chartAt(0).toUpperCase() + s.slice(1)).join(' ');
}
function titleCase(str){
return this.replace(/\w\S*/g, function(txt){return txt.chartAt(0).toUpperCase} + txt.substr(1).toLowerCase();});
};
Everytime I use the =>
it tells me that I need esversion:6
in order to run it.
My question, what am I doing wrong, and what would be the most idiomatic way to go about doing this?
Pictures to prove I'm not crazy: