SIGN IN SIGN UP

Algorithms and Data Structures implemented in JavaScript for beginners, following best practices.

34084 0 0 JavaScript
2021-12-11 13:30:26 +05:30
/**
* @function countVowels
* @description Given a string of words or phrases, count the number of vowels.
* @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')
}
const vowelRegex = /[aeiou]/gi
const vowelsArray = str.match(vowelRegex) || []
return vowelsArray.length
2021-12-11 13:30:26 +05:30
}
export { countVowels }