DEV Community

Cover image for JS Coding Question #8: Capitalize All Words In A Sentence [Not So Easy]๐Ÿ˜“๐Ÿ˜ฃ
Let's Code
Let's Code

Posted on • Updated on

JS Coding Question #8: Capitalize All Words In A Sentence [Not So Easy]๐Ÿ˜“๐Ÿ˜ฃ

Interview Question #8:

Write a function that will capitalize all words in a sentence.๐Ÿค”

If you need practice, try to solve this on your own. I have included 3๏ธโƒฃ potential solutions below.

Note: There are many other potential solutions to this problem.

Feel free to bookmark ๐Ÿ”– even if you don't need this for now. You may need to refresh/review down the road when it is time for you to look for a new role.

Codepen: If you want to play around and experiment with the code: https://codepen.io/angelo_jin/pen/xxrdBVE

Solution #1: Array Map (Recommended)

  • This is one solution I would go by if I am going to be asked in an interview. It pretty straight-forward approach and is legible. ๐Ÿ”ฅ
function capitalize(str) {
    return str
        .split(' ')
        .map(word => word[0].toUpperCase() + word.slice(1)).join(' ');
}
Enter fullscreen mode Exit fullscreen mode

Solution #2: Array Reduce (Recommended)

  • Another solution that I like. Array reduce is used as intended where one value is returned as a result of every iteration. Pretty sexy ๐Ÿ’ƒ approach just like array map above.
function capitalize(str) {
    return str
        .split(' ')
        .reduce((prev, current) => `${prev} ${current[0].toUpperCase() + current.slice(1)}`, '')
}
Enter fullscreen mode Exit fullscreen mode

Solution #3: for-of loop

  • Below is an alternative solution that can be used but it creates a variable to push results which array reduce solves easily. I see this as an OK option.
function capitalize(str) {
    const words = [];

    for (let word of str.split(' ')) {
        words.push(word[0].toUpperCase() + word.slice(1));
    }

    return words.join(' ');
}
Enter fullscreen mode Exit fullscreen mode

Happy coding and good luck if you are interviewing!

If you want to support me - Buy Me A Coffee

In case you like a video instead of bunch of code ๐Ÿ‘๐Ÿ˜Š

Top comments (5)

Collapse
 
sbaggott profile image
Sage Baggott

Love these JS coding questions!

A note on solution #2: instead of putting the whitespace between the prev and current props in the template string (Note: โฌœ๏ธ = whitespace so it's easier to see):

${prev}โฌœ๏ธ${current[0].toUpperCase() + current.slice(1)}, '')

...you should put the whitespace at the end of the template string:

${prev}${current[0].toUpperCase() + current.slice(1)}โฌœ๏ธ, '')

...that way you won't get an empty space before the first word.

Collapse
 
frontendengineer profile image
Let's Code

What is your solution of choice here? Please share if you have your own solution.

Collapse
 
lukaszlukasiewicz profile image
lukaszlukasiewicz

str.replace(/(^|\s)(.)/g,match => match.toUpperCase())

Collapse
 
frontendengineer profile image
Let's Code

that works beautifully! Thanks for the snippet.

Collapse
 
neillindberg profile image
neillindberg • Edited

This does the job, no slicing or anything needed.
const capitalize = (sentance) => sentance.toUpperCase();