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
  • 120,546
  • 16
  • 121
  • 140
2

Try:

function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);}
Kamil
  • 276
  • 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
  • 29,658
  • 5
  • 35
  • 59
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
  • 32,412
  • 7
  • 33
  • 72
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,216
  • 13
  • 24