function titleCase(str) {
str = str.trim().toLowerCase();
var table = str.split('');
table[0] = table[0].toUpperCase();
for (var index = 0; index < table.length; index++) {
if (table[index] === ' ') {
table[index + 1] = table[index + 1].toUpperCase();
// cl(wad);
}
}
return table.join('');
}
You can str.split(' ')
instead of str.split('')
and then use str.substr() or str.substring()
(See here)methods in your for-loop. Then just return table.join(' ')
.
So, you don’t have to iterate on each character of the str
param but just on each word on it, more faster code.