An Interest In:
Web News this Week
- April 25, 2024
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
August 25, 2021 09:55 pm GMT
Original Link: https://dev.to/frontendengineer/technical-interview-1-count-vowels-in-a-sentence-100k
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:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To