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 prob…


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

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!


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-08-25T21:55:41+00:00) Technical Interview #1: Count all vowels. Retrieved from https://www.scien.cx/2021/08/25/technical-interview-1-count-all-vowels/

MLA
" » Technical Interview #1: Count all vowels." Let's Code | Sciencx - Wednesday August 25, 2021, https://www.scien.cx/2021/08/25/technical-interview-1-count-all-vowels/
HARVARD
Let's Code | Sciencx Wednesday August 25, 2021 » Technical Interview #1: Count all vowels., viewed ,<https://www.scien.cx/2021/08/25/technical-interview-1-count-all-vowels/>
VANCOUVER
Let's Code | Sciencx - » Technical Interview #1: Count all vowels. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/25/technical-interview-1-count-all-vowels/
CHICAGO
" » Technical Interview #1: Count all vowels." Let's Code | Sciencx - Accessed . https://www.scien.cx/2021/08/25/technical-interview-1-count-all-vowels/
IEEE
" » Technical Interview #1: Count all vowels." Let's Code | Sciencx [Online]. Available: https://www.scien.cx/2021/08/25/technical-interview-1-count-all-vowels/. [Accessed: ]
rf:citation
» Technical Interview #1: Count all vowels | Let's Code | Sciencx | https://www.scien.cx/2021/08/25/technical-interview-1-count-all-vowels/ |

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.