How to count the number of each vowel in a string with JavaScript

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";
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' => 0, 'i' => 1, 'o' => 1, 'u' => 0 }

*/

 



answered Jul 17, 2024 by avibootz
edited Jul 17, 2024 by avibootz

Related questions

1 answer 123 views
1 answer 112 views
1 answer 103 views
1 answer 141 views
1 answer 93 views
1 answer 119 views
...