JS Built-ins
Algorithmsintermediate

Count Vowels

πŸ“– Usage Example
function countVowels(s) {
  return (s.match(/[aeiou]/gi) || []).length;
}
countVowels('Hello World'); // 3

Count vowels using a regex match that captures all vowels case-insensitively.

Count Vowels

Count the number of vowel characters (a, e, i, o, u) in a string, case-insensitive.

"hello" has 2 vowels: e, o
"aeiou" has 5 vowels
"xyz" has 0 vowels

Algorithm:

  1. Use a regular expression to match vowels
  2. Count the matches
function countVowels(s: string): number {
  // Return the count of vowels in s
}

countVowels('hello') // 2
πŸ’‘ Hints (5)
  • β€’ Use a regex pattern `/[aeiou]/gi` to match vowels.
  • β€’ The `g` flag finds all matches (not just the first).
  • β€’ The `i` flag makes it case-insensitive.
  • β€’ Use `match()` to find all matches.
  • β€’ Handle the case where `match()` returns null with `|| []`.