javascript

capitalizeWords()

Parameters: string

A single string input to be transformed

Returns: Transformed string with each word's first letter in uppercase

The capitalizeWords function is designed to take a string input and process that string to capitalize the first letter of every word it contains.

Strings
Loops
Conditionals
Medium dificulty

Short Title For The Blog Post That Explains How To Write A Code For The Function

Hello there, programmer. Welcome to our blog post, where we take a chill approach to breaking down some programming tasks. Today, we're going to delve into the workings of the capitalizeWords function in Javascript. Through the process, we aim for a clear, straightforward tutorial without unnecessary jargon. Sit back, relax, and enjoy the learning experience.

Step 1: Understand the problem

We need to write a Javascript function to capitalize the first character of each word in a string. We will create a function named capitalizeWords. This function will take one parameter - a string of words.

function capitalizeWords(str) {
  // We will write the code in the following steps
}

Step 2: Split string into words

Firstly, we need to split the string into individual words. We do this with the split(' ') method, which returns an array of words.

function capitalizeWords(str) {
  var words = str.split(' ');
}

Step 3: Capitalize the first letter

Then, for each word, we will capitalize the first letter. We use the toUpperCase() method to make the letter uppercase and concatenate it with the remainder of the word.

function capitalizeWords(str) {
  var words = str.split(' ');
  for (var i = 0; i < words.length; i++) {
    words[i] = words[i][0].toUpperCase() + words[i].substr(1);
  }
}

Step 4: Join words back into a string

After we have capitalized the first letter of each word, we join the words back into a single string with the join(' ') method.

function capitalizeWords(str) {
  var words = str.split(' ');
  for (var i = 0; i < words.length; i++) {
    words[i] = words[i][0].toUpperCase() + words[i].substr(1);
  }
  return words.join(' ');
}

Step 5: Conclusion

Thus, we now have a capitalizeWords Javascript function that will receive a string of words and return the same string but with each word starting with a capitalized letter.

Here's the complete, finalized function for your reference:

function capitalizeWords(str) {
  var words = str.split(' ');
  for (var i = 0; i < words.length; i++) {
    words[i] = words[i][0].toUpperCase() + words[i].substr(1);
  }
  return words.join(' ');
}

Learn function in:

String Capitalization

Transforms the first letter of every word in a string to uppercase

Learn more

Mathematical principle

The capitalizeWords function operates on the principle of string manipulation and traversal. It reviews every letter in a string, identifying spaces as word separators. Upon encountering a space, the character that follows is transformed into uppercase. The function uses the `toUpperCase()` method for this transformation.

Learn more