0

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...

  • This question had a specific case of its own since it called for using a `for ... of` loop to iterate over the data. I don't think it should be marked as a duplicate! – Joseph Ssebagala Mar 20 '18 at 13:51

6 Answers6

3
days.map(day => day[0].toUpperCase() + day.substr(1))
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
2

Try:

function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);}
Kamil
  • 286
  • 1
  • 9
0

Hope it helps

string.charAt(0).toUpperCase() + string.slice(1);
0

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));
}
Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
0

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);
Ele
  • 33,468
  • 7
  • 37
  • 75
0

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);
Omar Muscatello
  • 1,256
  • 14
  • 25