Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to capitalize the first character in array of strings, why this is not working?

I'm trying to write a function that converts for example list-style-image to listStyleImage.

I came up with a function but it seems not working. Can anybody point me to the problem here ?

var myStr = "list-style-image";

function camelize(str){
    var newStr = "";    
    var newArr = [];
    if(str.indexOf("-") != -1){
        newArr = str.split("-");
        for(var i = 1 ; i < newArr.length ; i++){
            newArr[i].charAt(0).toUpperCase();
        }       
        newStr = newArr.join("");
    }
    return newStr;
}

console.log(camelize(myStr));
like image 357
Rafael Adel Avatar asked Oct 12 '11 16:10

Rafael Adel


People also ask

How can you uppercase the first character in a string array?

To capitalize the first letter of each word in an array:Use the map() method to iterate over the array. On each iteration, use the toUpperCase() method on the first character of the word and concatenate the rest. The map method will return a new array with all words capitalized.

How do you capitalize a string array?

To convert all array elements to uppercase:Use the map() method to iterate over the array. On each iteration, call the toUpperCase() method to convert the string to uppercase and return the result. The map method will return a new array with all strings converted to uppercase.

How do you capitalize the first letter of a string?

To capitalize the first character of a string, We can use the charAt() to separate the first character and then use the toUpperCase() function to capitalize it.

How do you capitalize the first character of each word in a string?

In JavaScript, we have a method called toUpperCase() , which we can call on strings, or words. As we can imply from the name, you call it on a string/word, and it is going to return the same thing but as an uppercase. For instance: const publication = "freeCodeCamp"; publication[0].


1 Answers

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

days.map( a => a.charAt(0).toUpperCase() + a.substr(1) );
like image 166
Mostafa Elhusseiny Avatar answered Sep 25 '22 07:09

Mostafa Elhusseiny