How to count the number of each vowel in a string with Node.js

1 Answer

0 votes
function countNumberOfEachVowelInString(s) {
    const vowels = "aeiou";
    const countVowels = new Map();
     
    for (const ch of vowels) {
        countVowels.set(ch, 0);
    }
     
    return countVowels;
}
 
const s = "python c c++ c# java php javascript node.js";
const countVowels = countNumberOfEachVowelInString(s);
 
for (const ch of s) {
    if (countVowels.has(ch)) {
        countVowels.set(ch, countVowels.get(ch) + 1);
    }
}
 
console.log(countVowels);
 
 
 
/*
run:
 
Map(5) { 'a' => 4, 'e' => 1, 'i' => 1, 'o' => 2, 'u' => 0 }
 
*/

 



answered Jul 17, 2024 by avibootz

Related questions

1 answer 123 views
1 answer 102 views
1 answer 140 views
1 answer 93 views
1 answer 119 views
...