How to capitalize the first letter of each word in an array in JavaScript? -
this question has answer here:
i've been trying last hours understand why code not working well. instead of capitalizing first letters of each item in array code capitalizes letters in array.
function titlecase(str) { str = str.tolowercase().split(' '); (var = 0; < str.length; i++){ str[i] = str[i].split(' '); str[i][0] = str[i][0].touppercase(); str[i] = str[i].join(' '); } return str.join(' '); } titlecase("i'm little tea pot");
if want more functional way:
const titlecase = str => ( str.split(' ').map(c => c.slice(0, 1).touppercase() + c.slice(1)).join(' ') ); titlecase("i'm little tea pot");
Comments
Post a Comment