I initially have a string whose words first letter I want to capitalize. After capitalization, I'm unable to convert the array back to string. What's wrong here?
const name = 'This is a beautiful day';
console.log(name)
const capitalizedName = name.split(' ');
for (let i = 0; i < capitalizedName.length; i++) {
capitalizedName[i] =
capitalizedName[i][0].toUpperCase() + capitalizedName[i].substr(1);
}
capitalizedName.join(' ');
console.log(capitalizedName);
The output:-
This is a beautiful day
[ 'This', 'Is', 'A', 'Beautiful', 'Day' ]
Output that I was expecting:-
This is a beautiful day
This Is A Beautiful Day
Array.prototype.join()returns the joined string. You're not doing anything with its return value.console.log(('This is a beautiful day').split(' ').map(w => w[0].toUpperCase() + w.slice(1)).join(' '));. ThecapitalizedName.join(' ');returns a string - it does not mutate the arraycapitalizedName. So, if you simply sayconsole.log(capitalizedName.join(' '));- it should provide the desired result.