2021-12-11 13:30:26 +05:30
|
|
|
/**
|
|
|
|
|
* @function countVowels
|
|
|
|
|
* @description Given a string of words or phrases, count the number of vowels.
|
2022-02-17 18:00:04 +06:00
|
|
|
* @param {String} str - The input string
|
|
|
|
|
* @return {Number} - The number of vowels
|
2021-12-11 13:30:26 +05:30
|
|
|
* @example countVowels("ABCDE") => 2
|
|
|
|
|
* @example countVowels("Hello") => 2
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
const countVowels = (str) => {
|
|
|
|
|
if (typeof str !== 'string') {
|
|
|
|
|
throw new TypeError('Input should be a string')
|
|
|
|
|
}
|
2022-02-17 18:00:04 +06:00
|
|
|
|
|
|
|
|
const vowelRegex = /[aeiou]/gi
|
|
|
|
|
const vowelsArray = str.match(vowelRegex) || []
|
|
|
|
|
|
|
|
|
|
return vowelsArray.length
|
2021-12-11 13:30:26 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export { countVowels }
|