Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions String/CountVowels.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* @function countVowels
* @description Given a string of words or phrases, count the number of vowels.
* @param {String} url - The input string
* @return {Number} count
* @param {String} str - The input string
* @return {Number} - The number of vowels
* @example countVowels("ABCDE") => 2
* @example countVowels("Hello") => 2
*/
Expand All @@ -11,15 +11,11 @@ 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

const vowelRegex = /[aeiou]/gi
const vowelsArray = str.match(vowelRegex) || []

return vowelsArray.length
}

export { countVowels }