Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 25, 2021 09:55 pm GMT

Technical Interview 1: Count all vowels

Interview Question #1:

Write a function that counts all vowels 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.

Solution #1: String match method

function getVowelsCount(sentence) {  return sentence.match(/[aeuio]/gi) ? sentence.match(/[aeuio]/gi).length : 0;}

Solution #2: for-of And regex

function getVowelsCount (sentence) {    let vowelsCount = 0    const vowels = ['a', 'e', 'i', 'o', 'u']    for (let char of sentence) {        if (/[aeiou]/gi.test(char.toLowerCase())) {            vowelsCount++        }    }    return vowelsCount}

Solution #3: for-of AND array.includes

function getVowelsCount (sentence) {    let vowelsCount = 0    const vowels = ['a', 'e', 'i', 'o', 'u']    for (let char of sentence) {        if (vowels.includes(char.toLowerCase())) {            vowelsCount++        }    }    return vowelsCount}

In case you like a video instead of bunch of code

Happy coding and good luck if you are interviewing!


Original Link: https://dev.to/frontendengineer/technical-interview-1-count-vowels-in-a-sentence-100k

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To