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 content originally appeared on DEV Community and was authored by Let's Code

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(' ');
}

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)}`, '')
}

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(' ');
}

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 ??


This content originally appeared on DEV Community and was authored by Let's Code


Print Share Comment Cite Upload Translate Updates
APA

Let's Code | Sciencx (2021-09-14T19:16:29+00:00) JS Coding Question #8: Capitalize All Words In A Sentence [Not So Easy]??. Retrieved from https://www.scien.cx/2021/09/14/js-coding-question-8-capitalize-all-words-in-a-sentence-not-so-easy%f0%9f%98%93%f0%9f%98%a3/

MLA
" » JS Coding Question #8: Capitalize All Words In A Sentence [Not So Easy]??." Let's Code | Sciencx - Tuesday September 14, 2021, https://www.scien.cx/2021/09/14/js-coding-question-8-capitalize-all-words-in-a-sentence-not-so-easy%f0%9f%98%93%f0%9f%98%a3/
HARVARD
Let's Code | Sciencx Tuesday September 14, 2021 » JS Coding Question #8: Capitalize All Words In A Sentence [Not So Easy]??., viewed ,<https://www.scien.cx/2021/09/14/js-coding-question-8-capitalize-all-words-in-a-sentence-not-so-easy%f0%9f%98%93%f0%9f%98%a3/>
VANCOUVER
Let's Code | Sciencx - » JS Coding Question #8: Capitalize All Words In A Sentence [Not So Easy]??. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/14/js-coding-question-8-capitalize-all-words-in-a-sentence-not-so-easy%f0%9f%98%93%f0%9f%98%a3/
CHICAGO
" » JS Coding Question #8: Capitalize All Words In A Sentence [Not So Easy]??." Let's Code | Sciencx - Accessed . https://www.scien.cx/2021/09/14/js-coding-question-8-capitalize-all-words-in-a-sentence-not-so-easy%f0%9f%98%93%f0%9f%98%a3/
IEEE
" » JS Coding Question #8: Capitalize All Words In A Sentence [Not So Easy]??." Let's Code | Sciencx [Online]. Available: https://www.scien.cx/2021/09/14/js-coding-question-8-capitalize-all-words-in-a-sentence-not-so-easy%f0%9f%98%93%f0%9f%98%a3/. [Accessed: ]
rf:citation
» JS Coding Question #8: Capitalize All Words In A Sentence [Not So Easy]?? | Let's Code | Sciencx | https://www.scien.cx/2021/09/14/js-coding-question-8-capitalize-all-words-in-a-sentence-not-so-easy%f0%9f%98%93%f0%9f%98%a3/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.