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>
<html>
<head>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>

</body>
</html>

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
    x++
  vcount

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.

Help the community:

PHP: Loop

JavaScript: Delete duplicates in an array

Oracle: Round the number