How to capitalize the first letter of each word in an array in JavaScript? -


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

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -