w3resource logo

CoffeeScript exercises

CoffeeScript function: Counts the number of vowels within a string

CoffeeScript Function : Exercise-14 with Solution

Write a CoffeeScript function that accepts a string as a parameter and counts the number of vowels within the string.

Note : As the letter 'y' can be regarded as both a vowel and a consonant, we do not count 'y' as a vowel here.
Example string : 'The quick brown fox'
Expected Output : 5

Solution :

HTML Code :

<!DOCTYPE html>
  <meta charset="utf-8">
  <title>JS Bin</title>


CoffeeScript Code :

vowel_count = (str1) ->
  vowel_list = 'aeiouAEIOU'
  vcount = 0
  x = 0
  while x < str1.length
    if vowel_list.indexOf(str1[x]) != -1
      vcount += 1

alert vowel_count('The quick brown fox')

Live Demo :

JS Bin on jsbin.com

Post your code through Disqus :

Join our Question Answer community to learn and share your programming knowledge.