GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — merge: optimize the countVowels algo (#886) · ex1234/JavaScript@68ca0ce

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • String
    • CountVowels.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎String/CountVowels.js‎

Copy file name to clipboard
+7-11Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* @function countVowels
33
* @description Given a string of words or phrases, count the number of vowels.
4-
* @param {String} url - The input string
5-
* @return {Number} count
4+
* @param {String} str - The input string
5+
* @return {Number} - The number of vowels
66
* @example countVowels("ABCDE") => 2
77
* @example countVowels("Hello") => 2
88
*/
@@ -11,15 +11,11 @@ const countVowels = (str) => {
1111
if (typeof str !== 'string') {
1212
throw new TypeError('Input should be a string')
1313
}
14-
const vowels = new Set(['a', 'e', 'i', 'o', 'u'])
15-
let count = 0
16-
for (let i = 0; i < str.length; i++) {
17-
const char = str[i].toLowerCase()
18-
if (vowels.has(char)) {
19-
count++
20-
}
21-
}
22-
return count
14+
15+
const vowelRegex = /[aeiou]/gi
16+
const vowelsArray = str.match(vowelRegex) || []
17+
18+
return vowelsArray.length
2319
}
2420

2521
export { countVowels }

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.