Skip to content

Instantly share code, notes, and snippets.

@luisangelma
Created July 16, 2015 19:03
Show Gist options
  • Select an option

  • Save luisangelma/cded0621ba72ff104048 to your computer and use it in GitHub Desktop.

Select an option

Save luisangelma/cded0621ba72ff104048 to your computer and use it in GitHub Desktop.
Vowels and Consonants
function getVowels(str) {
var numberOfVowels = 0;
var strArray = str.split('');
for (var i = 0; i < strArray.length; i++ ) {
switch(strArray[i]) {
case 'a':
numberOfVowels++;
break;
case 'e':
numberOfVowels++;
break;
case 'i':
numberOfVowels++;
break;
case 'o':
numberOfVowels++;
break;
case 'u':
numberOfVowels++;
break;
}
}
console.log('The total number of vowels is: ' + numberOfVowels);
}
function getConsonants(str) {
var numberOfConsonants = 0;
for (var i = 0; i < str.length; i++ ) {
if (str.charAt(i) === 'a' || str.charAt(i) === 'e' || str.charAt(i) === 'i' || str.charAt(i) === 'o' || str.charAt(i) === 'u' || str.charAt(i) === ' ') {
numberOfConsonants;
} else {
numberOfConsonants++;
}
}
console.log('The total number of consonants is: ' + numberOfConsonants);
}
getVowels('coding campus');
getConsonants('coding campus');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment