I have my code, I just do not know how to end it ...
const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
for (day of days) {
console.log(day);
}
I have my code, I just do not know how to end it ...
const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
for (day of days) {
console.log(day);
}
If you just want to print, you can use .toUpperCase()
and .substr()
:
const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
for (day of days) {
console.log(day[0].toUpperCase()+day.substr(1));
}
The day[0].toUpperCase()
converts the first letter to uppercase, and day.substr(1)
returns the second character to the end. Then just concatenate both.
Another interesting way to get the same result is with toUpperCase
and slice
. The slice
serves to get the rest of the string without catching the first letter:
const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
for (day of days) {
console.log(day[0].toUpperCase() + day.slice(1));
}
Curiously, you can even do it with a simple regex, which takes the first letter and replaces it with the uppercase version at the cost of toUpperCase
:
const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
for (day of days) {
console.log(day.replace(/\w/, letra => letra.toUpperCase()));
}
The% regex% is what picks up the first letter for the transformation.