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));
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.
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.
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.
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].
const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];
days.map( a => a.charAt(0).toUpperCase() + a.substr(1) );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With