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

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/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.