Skip to content

Instantly share code, notes, and snippets.

@pankajladhar
Created January 13, 2018 07:26
Show Gist options
  • Save pankajladhar/592e77db8b9c888ae2a7cfec0583afe7 to your computer and use it in GitHub Desktop.
Save pankajladhar/592e77db8b9c888ae2a7cfec0583afe7 to your computer and use it in GitHub Desktop.
Count vowels available in string
/*
VowelCount is javascript function which takes string as input
return count of vowels available or 0 if no vowels available
*/
const VowelCount = (str) =>{
const vowelArray = ['a', 'e', 'i', 'o', 'u'];
let counter = 0;
for (let i = 0; i < vowelArray.length; i++) {
for (let j = 0; j < str.length; j++) {
str[j].toLowerCase() == vowelArray[i] && ++ counter
}
}
return counter;
}
const VowelCount_Regex = (str) =>{
return str.match(/a|e|i|o|u/gi) ? str.match(/a|e|i|o|u/gi).length : 0;
}
console.log(VowelCount("All cows eat grass")) //5
console.log(VowelCount("Bxt")) // 0
console.log(VowelCount_Regex("All cows eat grass")) // 5
console.log(VowelCount_Regex("Bxt")) // 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment