Skip to content
WANCODE
BM

Count the Vowels

10 minBeginnerAlgorithms

Write a function that counts how many vowels a piece of text contains. It's a small problem that practises looping over a string, comparing characters and handling upper/lower case.

JavaScript
countVowels("kopi")                   // 2
countVowels("WANCode")                // 3  (A, o, e)
countVowels("Selamat Pagi Malaysia")  // 9
countVowels("rhythm")                 // 0
countVowels("")                       // 0

Examples

Constraints

  • Vowels are exactly a e i o u in either case. y is not a vowel here.
  • Spaces, digits and punctuation are ignored.
  • Return a number, not a string.

Implement countVowels(text) so it returns the number of vowels (a e i o u, upper or lower case) in text — as a number.

Checks

  • Counts vowels in lowercase words
  • Counts uppercase vowels too
  • Returns 0 when there are no vowels
Preview

Console output from your code appears here.

Step 1 of 1