Count Vowels
Challenge: Count Vowels 🔢
Why this matters: text analysis, search ranking, and even simple spell‑check tools rely on being able to inspect and classify characters efficiently.
Count how many vowels (a, e, i, o, u) are in a string.
Examples
countVowels("hello"); // 2
countVowels("JavaScript"); // 3
countVowels("xyz"); // 0
Approaches
Method 1: Loop through characters
const vowels = "aeiou";
let count = 0;
for (let char of str.toLowerCase()) {
if (vowels.includes(char)) {
count++;
}
}
Method 2: Filter
str.split("").filter(c => "aeiou".includes(c.toLowerCase())).length
Method 3: Match with regex
(str.match(/[aeiou]/gi) || []).length
Your Turn!
Count the vowels.