Capitalize The First Letter Challenge

// — — 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.

// — — Examples

// capitalize(‘a short sentence’) → ‘A Short Sentence’

// capitalize(‘a lazy fox’) → ‘A Lazy Fox’

// capitalize(‘look, it is working!’) → ‘Look, It Is Working!’

This challenge is good for two things.

This challenge isn’t particularly mind boggling, but it does take some time to think about how you’re going to go about solving it.

Let’s talk about it in pseudo code.

So what do we need to do? We need to write a function that takes in a string and returns a new string where the first letter of each word will be capitalized. I solved this problem a couple of different ways and I will break down each solution step-by-step.

Transforming -

If we want to iterate through the string, we can use a standard for loop or a for of loop on the string.

I decided I thought it would be easier to deal with an array than a string because strings are immutable.

Here are my first steps:

let finalArray = []

let arr = str.split(‘ ‘)

I created an empty array to push my capitalized words into, and split my string into an array to perform actions on it. (notice there is a space between quotations in my split argument. This means the items in the array will be split between the spaces in our input string.)

The Loop -

The next thing I want to do is create a loop so that we can loop through each item in our array to perform actions on it.

for (let word of arr) {

}

The tricky part of this challenge is understanding that in order to have access to the first letter of each word, we have to split the word into individual letters.

let letters = word.split(‘’) // meaning ‘hello’ turns into [‘h’, ‘e’, ‘l’, ‘l’, ‘o’]

Now that we have the word split up, we can capitalize the first letter of each word.

letters[0].toUpperCase()

Now it’s time to join the word together in its new form.

letters[0].toUpperCase() + word.slice(1)

and since we’re pushing it into a new array, we can actually push those concatenated strings at the same time in our push argument

finalArray.push(letters[0].toUpperCase() + word.slice(1))

When the for loop ends, we want to return our new array, but it needs to be joined between the spaces the same way that it was split between its spaces

return finalArray.join(‘ ‘)

This solution passes all of the tests. It’s efficient, very readable, and does exactly what it’s supposed to.

Second Solution -

The next solution it’s exactly semantic, but it is a one liner solution.

return str.split(‘ ‘).map(word => word[0].toUpperCase() + word.slice(1)).join(‘ ‘)

It’s essentially the same exact thing, minus using the new array to allocate the memory.

Split it into an array of words, map each word and return a new string where the first letter is capitalized.

That’s pretty much the whole problem. Like I said, it isn’t exactly a mind boggling problem, but it can get a little tricky when it comes to implementation.

I hope this has been a helpful technical blog post! I really enjoy writing these posts because I find passion in teaching basic coding concepts to new software engineers. I’ll be doing a harder code challenge next. Thanks for reading!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store