Maybe there is a risk that the very same authority which is asking you to count vowels will soon ask you to count consonants, just to have you see how flexible your code is.
So it could be a good idea to start with a function that takes two arguments:
- the string under test and
- the set of accepted vowels.
Like function countCharsFromVowelSet() in the below code snippet.
Note that deciding what exactly is an acceptable vowel is a language and country dependent decision.
const countCharsFromVowelSet = function(str, vowelSet) {
let arr = [...str];
let count = (arr.filter(c => vowelSet.includes(c))).length;
return count;
};
/* auxiliary function builder function: */
const makeCharCounter = function(charSet) {
return (str => countCharsFromVowelSet(str, charSet));
};
const EnglishVowelList = "AEIOUaeiou""AEIOUaeiou";
const GermanVowelList = "AEIOUYÄÖÜaeiouyäöü""AEIOUYÄÖÜaeiouyäöü";
const countEnglishVowels = makeCharCounter(EnglishVowelList);
const countGermanVowels = makeCharCounter(GermanVowelList);
text1 = "William Shakespeare";
count1 = countEnglishVowels(text1);
text2 = "Die Schöpfung";
count2 = countGermanVowels(text2);
console.log("There are " + count1.toString() + " vowels in: " + text1);
console.log("There are " + count2.toString() + " vowels in: " + text2);