const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
for (const day of days) {
console.log(day);
}
I need to print the days with the first letters capitalized...
const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
for (const day of days) {
console.log(day);
}
I need to print the days with the first letters capitalized...
days.map(day => day[0].toUpperCase() + day.substr(1))
Try:
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);}
Hope it helps
string.charAt(0).toUpperCase() + string.slice(1);
You can simply loop over the days and get the first character to uppercase like this:
const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
for (const day of days) {
console.log(day[0].toUpperCase() + day.substr(1));
}
Using the function map
and the regex /(.?)/
to replace the captured first letter with its upperCase representation.
const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
var result = days.map(d => d.replace(/(.?)/, (letter) => letter.toUpperCase()));
console.log(result);
Old school:
const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
var result = [];
for(var i = 0; i < days.length; i++){
result.push(days[i].charAt(0).toUpperCase() + days[i].substring(1));
}
console.log(result);