javascriptbeginner10 minutes

Beginner Challenge: Create a Function to Capitalize the First Letter of Each Word

Practice string manipulation by writing a JavaScript function that capitalizes the first letter of every word in a given sentence.

Challenge prompt

Write a function named 'capitalizeWords' that takes a single string as input and returns a new string with the first letter of each word capitalized. Words are separated by spaces. For example, 'hello world' should become 'Hello World'.

Guidance

  • Split the input string into an array of words using the space character as the separator.
  • Capitalize the first character of each word and concatenate it with the rest of the word.
  • Join the capitalized words back together with spaces to form the final string.

Hints

  • You can use the 'split' method on strings to create an array of words.
  • Use 'charAt(0)' to get the first letter of each word, and 'toUpperCase()' to capitalize it.
  • Remember to add the rest of the word after the capitalized first letter.

Starter code

function capitalizeWords(str) {
  // Your code here
}

Expected output

capitalizeWords('hello world') // 'Hello World'

Core concepts

string manipulationfunctionsarrays

Challenge a Friend

Send this duel to someone else and see if they can solve it.