Write a function that counts the number of vowels (a, e, i, o, u) in a given string.
Input: str = "Write a program to count the number of vowels in a string."
Output: 17
function countVowelsUsingForEach(str) {
const lowerString = str.toLowerCase();
const vowels = ["a", "e", "i", "o", "u"];
let vowelCount = 0;
lowerString.split("").forEach((char) => {
if (vowels.includes(char)) vowelCount++;
});
return vowelCount;
}
const str1 = "Write a program to count the number of vowels in a string.";
countVowelsUsingForEach(str1); //output: 17
function countVowelsUsingFilter(str) {
const lowerString = str.toLowerCase();
const vowels = ["a", "e", "i", "o", "u"];
return lowerString.split("").filter((char) => vowels.includes(char)).length;
}
const str1 = "Write a program to count the number of vowels in a string.";
countVowelsUsingFilter(str1); //output: 17
function countVowelsUsingRegex(str) {
const lowerString = str.toLowerCase();
const vowelRegex = /[aeiou]/g;
const matches = lowerString.match(vowelRegex);
return matches ? matches.length : 0;
}
const str1 = "Write a program to count the number of vowels in a string.";
countVowelsUsingRegex(str1); //output: 17
function countVowelsUsingSwitch(str) {
const lowerString = str.toLowerCase();
let vowelCount = 0;
for (let i = 0; i < lowerString.length; i++) {
switch (lowerString[i]) {
case "a":
case "e":
case "i":
case "o":
case "u":
vowelCount++;
break;
default:
continue;
}
}
return vowelCount;
}
const str1 = "Write a program to count the number of vowels in a string.";
countVowelsUsingSwitch(str1); //output: 17
function countVowelsUsingIndexOf(str) {
const lowerString = str.toLowerCase();
const vowels = "aeiou";
let vowelCount = 0;
for (let i = 0; i < lowerString.length; i++) {
if (vowels.indexOf(lowerString[i]) !== -1) vowelCount++;
}
return vowelCount;
}
const str1 = "Write a program to count the number of vowels in a string.";
countVowelsUsingIndexOf(str1); //output: 17
function countVowelsUsingReduce(str) {
const lowerString = str.toLowerCase();
const vowels = "aeiou";
return lowerString.split("").reduce((count, char) => {
return vowels.includes(char) ? count + 1 : count;
}, 0);
}
const str1 = "Write a program to count the number of vowels in a string.";
countVowelsUsingReduce(str1); //output: 17
function countVowelsUsingArrayFrom(str) {
const lowerString = str.toLowerCase();
const vowels = "aeiou";
return Array.from(lowerString).filter(char => vowels.includes(char)).length;
}
const str1 = "Write a program to count the number of vowels in a string.";
countVowelsUsingArrayFrom(str1); //output: 17
function countVowelsUsingMapReduce(str) {
const lowerString = str.toLowerCase();
const vowels = "aeiou";
return lowerString
.split("")
.map(char => vowels.includes(char) ? 1 : 0)
.reduce((total, count) => total + count, 0);
}
const str1 = "Write a program to count the number of vowels in a string.";
countVowelsUsingMapReduce(str1); //output: 17
function countVowelsUsingSome(str) {
const lowerString = str.toLowerCase();
const vowels = "aeiou";
let vowelCount = 0;
lowerString.split("").forEach(char => {
if (vowels.split("").some(vowel => vowel === char)) vowelCount++;
});
return vowelCount;
}
const str1 = "Write a program to count the number of vowels in a string.";
countVowelsUsingSome(str1); //output: 17
function countVowelsUsingForOf(str) {
const lowerString = str.toLowerCase();
const vowels = "aeiou";
let vowelCount = 0;
for (const char of lowerString) {
if (vowels.includes(char)) vowelCount++;
}
return vowelCount;
}
const str1 = "Write a program to count the number of vowels in a string.";
countVowelsUsingForOf(str1); //output: 17