Sequence NODE_145
EasyCount Vowels in a String
JavaScript
Technical Specification
Count the number of vowels (a, e, i, o, u) present in a given string.
Input/Output Samples
Input:"happy"
Output:1
Input:"CodeWithHappy"
Output:4
Optimal Logic Path
function countVowels(str) {
const vowels = "aeiouAEIOU";
let count = 0;
for (const ch of str) {
if (vowels.includes(ch)) count++;
}
return count;
}Architectural Deep-Dive
We scan each character and increment the counter if it is in the defined set of vowels.