Index

Assignment 21 CB - Vowel count

Using the JavaScript language, have the function vowelCount(str) take the str string parameter being passed and return the number of vowels the string contains (ie. "All cows eat grass" would return 5). Do not count y as a vowel for this challenge.

Enter some numbers, separated by comma, in the field below to calculate their sum and product.

$(document).ready(function() { function vowelCount(str) { var vowels = ['a','e','i','o','u'], vowelsCount = 0, len; str = str.split(''); len = str.length; for (var i = 0; i < len; i++){ if (vowels.indexOf(str[i].toLowerCase()) !== -1) { vowelsCount++; } } return vowelsCount; } $('.sort').click(function() { var string = $('.data input').val(), message = ''; if (string === '') { message = 'Please enter a string in the field.'; } else { message = 'Number of vowels in the string is: <span>' + vowelCount(string) + '</span>'; } $('.result p ').html(message); }); });