• Register
100 points
7 4

Capitalizing the first letter of the word is the most common question, asked in programming quizzes. There are more than one method of doing  that. Different people can use different methods to capitalize the first letter of the word according to  their will. I’ll be discussing only one method in  this article, and that will be easiest in understanding.

Approach

Javascript provides an built-in toUppercase()  function to capitalize words. That function supports most of the web browser. toUppercase() function converts all the letters to uppercase, but we want to change only the first letter. So for doing that we have to modify the line of code. This function only takes input as a parameter and changes all the letters to uppercase.

  • Create a function for capitalisation
  • Split all the letters
  • Create an empty array for capitalized words
  • Call the toUppercase() function to capitalize first letter of the word
function capitalize(input) {  
    var words = input.split(' ');  
    var CapitalizedWords = [];  
    words.forEach(element => {  
        CapitalizedWords.push(element[0].toUpperCase() + element.slice(1, element.length));  
    });  
    return CapitalizedWords.join(' ');  
}  

 

Output

capitalize(‘hey!!! this is an test’)
“Hey!!! This Is An test”

Hope this will help.

100 points
7 4