merge: count Vowels (#864)

This commit is contained in:
YATIN KATHURIA
2021-12-11 13:30:26 +05:30
committed by GitHub
parent 4d55b40c9e
commit c30b897324
3 changed files with 48 additions and 44 deletions

25
String/CountVowels.js Normal file
View File

@ -0,0 +1,25 @@
/**
* @function countVowels
* @description Given a string of words or phrases, count the number of vowels.
* @param {String} url - The input string
* @return {Number} count
* @example countVowels("ABCDE") => 2
* @example countVowels("Hello") => 2
*/
const countVowels = (str) => {
if (typeof str !== 'string') {
throw new TypeError('Input should be a string')
}
const vowels = new Set(['a', 'e', 'i', 'o', 'u'])
let count = 0
for (let i = 0; i < str.length; i++) {
const char = str[i].toLowerCase()
if (vowels.has(char)) {
count++
}
}
return count
}
export { countVowels }