How to make Javascript capitalize each element of an array?

0

I have this function and this for.. of but I can not get the days to be capitalized by console.

What's wrong ???

Array.prototype.toUpperCase = function() {
  for (let i = 0; i < days.length; i++) {
        days[i] = days[i].charAt(0).toUpperCase() + days[i].substr(1);

  }
};
const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']; 

for (const day of days) {
  console.log(day);
}
    
asked by Cata 07.12.2017 в 17:20
source

4 answers

1

I see you're trying to convert the first letter into a capital letter, which outside of the function you're declaring is enough just to put the for where the first letter you pass it to capital and add the slice of the string from the character with index 1 in the chain.

/*Array.prototype.toUpperCase = function() {
  for (let i = 0; i < days.length; i++) {
        days[i] = days[i].charAt(0).toUpperCase() + days[i].slice(1);

  }
};*/
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);

  }

for (const day of days) {
  console.log(day);
}
    
answered by 07.12.2017 / 17:32
source
0

You must change the way you declare the variable days , you are using cosnt of es6, and it does not allow the value of the array elements to change, use let in your place.

Execute days.toUpperCase() before running the last for :

Array.prototype.toUpperCase = function() {
  for (let i = 0; i < days.length; i++) {
        days[i] = days[i].charAt(0).toUpperCase() + days[i].slice(1);

  }
};
const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']; 

days.toUpperCase()
for (let i = 0; i < days.length; i++) {
        days[i] = days[i].charAt(0).toUpperCase() + days[i].slice(1);

  }
    
answered by 07.12.2017 в 17:30
0

2 things happen with your code

  • The first is that you never call the toUppercase method even though you're assigning it.

  • The variable days is not present or is not in the context of the function.

  • I'll let you solve the problem

    Array.prototype.toUpperCase = function() {
      return this.map(word => word.toUpperCase())
    };
    const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'].toUpperCase(); 
    for (const day of days) {
      console.log(day);
    }

    This way you do not cling to a single variable and you can always perform a toUpperCase to any array with similar characteristics.

        
    answered by 07.12.2017 в 17:36
    0
      

    You can use this option as a quick method.

    const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
    
    String.prototype.capitalize = function() {
        return this.toLowerCase().replace(/(^|\s)([a-z])/g, function(m, p1, p2) { return p1 + p2.toUpperCase(); });
    }
    
    days.map((day) => {    
        console.log(day.capitalize())
    })
        
    answered by 07.12.2017 в 18:39