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

1 Answer

0 votes
function countNumberOfEachVowelInString(s: string) {
    const vowels: string = "aeiou";
    const countVowels: Map<any, any> = new Map();
     
    for (const ch of vowels) {
        countVowels.set(ch, 0);
    }
     
    return countVowels;
}
 
const s: string = "python c c++ c# java php javascript typescript";
const countVowels: Map<any, any> = 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" => 2, "o" => 1, "u" => 0}
 
*/

 



answered Jul 17, 2024 by avibootz

Related questions

1 answer 206 views
1 answer 208 views
1 answer 196 views
1 answer 227 views
1 answer 194 views
1 answer 237 views
...