-5
const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
   
for(let day of days){       
    console.log(day[0]);
}

This is enables me get a hold of the first letter of each word in the array, but what manipulation can i do to capitalize it. I have already tried the day[0].toUpperCase() method.

GIGO
  • 68
  • 9
  • 2
    Have a look at `chatAt(0)` https://www.w3schools.com/jsref/jsref_charat.asp – Chris Walsh Dec 16 '17 at 01:15
  • 2
    Possible duplicated of https://stackoverflow.com/questions/1026069/how-do-i-make-the-first-letter-of-a-string-uppercase-in-javascript – Ciro Spaciari Dec 16 '17 at 01:16
  • 1
    Possible duplicate of [How do I make the first letter of a string uppercase in JavaScript?](https://stackoverflow.com/questions/1026069/how-do-i-make-the-first-letter-of-a-string-uppercase-in-javascript) – Seth McClaine Dec 16 '17 at 01:19
  • `I have already tried the day[0].toUpperCase() method` - not in the code you posted you haven't!! – Jaromanda X Dec 16 '17 at 01:26

3 Answers3

3

day[0].toUpperCase() will uppercase the first letter of the day, but it doesn't mutate the string you call it on, it returns a new string. So what you need to do is capture that result, and add the rest of the orignal string to it. See the following snippet:

var day = "monday"

// day[0].toUpperCase() returns a new string:
console.log("day[0].toUpperCase() is:", day[0].toUpperCase())

// You can use the substring method to get the rest of the string.
console.log("day.substring(1) is:", day.substring(1));

// Use these two in combination to uppercase the first letter:
console.log("combo:", day[0].toUpperCase() + day.substring(1));

// And save that into a new variable:
var capDay = day[0].toUpperCase() + day.substring(1);

// But note that the orignal string is still unchanged.
console.log("The original is still unchanged. Original:", day);
console.log("But the new variable is different:", capDay);

You should be able to figure out the rest from there.

CRice
  • 29,968
  • 4
  • 57
  • 70
0

JSFiddle: https://jsfiddle.net/g9c42oc8/

const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];    
for(let day of days){       
    console.log(day.charAt(0).toUpperCase() + day.slice(1));
}
Seth McClaine
  • 9,142
  • 6
  • 38
  • 64
0

Simple way to capitalize and log the values of the array days:

const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
for ( let i = 0; i < days.length; i++ ) {
  days[i] = days[i].charAt(0).toUpperCase() + days[i].slice(1);;
  console.log(days[i]);
}
user7393973
  • 2,270
  • 1
  • 20
  • 58