- Published on
Capitalize First Letter - JavaScript Algorithms
453 words3 min read
- Authors
- Name
- Curtis Warcup
Directions: Write a function that accepts a string. The function should capitalize the first letter of each word in the string then return the capitalized string.
Example:
capitalize('a short sentence') --> 'A Short Sentence'
capitalize('a lazy fox') --> 'A Lazy Fox'
capitalize('look, it is working!') --> 'Look, It Is Working!'
Solution 1: slice()
We can use the built in javascript method slice(startIndex, endIndex)
and concatenate the first letter to the rest of the string.
- Make an empty array to contain the capitalized words.
- Use
split()
on the input string to split the string into an array of words, splitting on spaces. - For each word in the array...
- Use
toUpperCase()
on the first letter of the word. - Join first letter with rest of the string
- Push results into the empty array.
- Use
- Join the array containing the capitalized words into a string and return the result.
function capitalize(str) {
let words = []
for (let word of str.split(' ')) {
words.push(word[0].toUpperCase() + word.slice(1))
}
return words.join(' ')
}
Solution 2: For...of Loop
- Initialize a string with the capitalized version of the first letter of the input string.
- For each character in the string...
- IF the character to the left is a space
- use
toUpperCase()
on the character - push it into the string containing the capitalized words.
- use
- ELSE
- push the character into the string containing the capitalized words.
- IF the character to the left is a space
- Return the newly created string.
function capitalize(str) {
let result = str[0].toUpperCase()
for (let i = 1; i < str.length; i++) {
if (str[i - 1] === ' ') {
result += str[i].toUpperCase()
} else {
result += str[i]
}
}
return result
}