Question Bank
JavaScript Vowel Counting: Three Approaches Quiz
Difficulty: Medium
Count vowels in a string three different ways (loop + Set lookup, split + forEach, regex match) plus two companion drills on case sensitivity and big-O analysis.
JavaScript Vowel Counting: Three Approaches Quiz
Count vowels in a string three different ways (loop + Set lookup, split + forEach, regex match) plus two companion drills on case sensitivity and big-O analysis.
473 views
7
Implement findVowels(str) using a loop and an explicit vowel-set lookup. The function should be case-insensitive and return the total count of a, e, i, o, u.
Examples
Example 1:
Input: 'hello'
Output: 2
Explanation: 'e' and 'o' are vowels.Example 2:
Input: 'BCDFG'
Output: 0
Explanation: No vowels in any case, so the count is 0.Now implement the same function with split('') plus forEach and a short-circuit && increment. Explain why this version is shorter but allocates more memory.
Examples
Example 1:
Input: 'JavaScript'
Output: 3
Explanation: 'a', 'a', 'i' are vowels (the leading 'J' is consonant in upper case but normalization handles it).Implement the same vowel counter using a single regular expression with the g and i flags. Why does the match call here need a fallback to 0?
Examples
Example 1:
Input: 'Programming'
Output: 3
Explanation: 'o', 'a', 'i' all match /[aeiou]/gi.Example 2:
Input: 'rhythm'
Output: 0
Explanation: No vowels means `match` returns null, so the function must fall back to 0.Edge case: a basic vowel counter built with /[aeiou]/gi only counts ASCII English vowels. Extend the regex-based version to ALSO count accented Latin vowels (a, e, i, o, u with grave/acute/circumflex/diaeresis) using a Unicode-aware regex.
Examples
Example 1:
Input: 'résumé'
Output: 3
Explanation: 'e', 'u' and 'e' (the two accented 'e' and the 'u') all count once the regex handles diacritics.Example 2:
Input: 'naïve'
Output: 3
Explanation: 'a', 'i' (with diaeresis), and 'e' each match.Big-O question: which of the three original approaches (loop, split+forEach, regex) is the most memory-efficient for a 10 MB string, and why? Pick one and justify your answer in 2-3 sentences.
Examples
Example 1:
Input: a 10 MB string in Node.js with --max-old-space-size set tightly
Output: the loop version (`for ... of` on `str.toLowerCase()`)
Explanation: It avoids the O(n) intermediate array allocation that split + forEach creates while still being O(n) time.