Skip to content

Instantly share code, notes, and snippets.

@eldyvoon
Created January 5, 2018 03:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eldyvoon/35f2d756953b1f66beec81752b8c2ca0 to your computer and use it in GitHub Desktop.
Save eldyvoon/35f2d756953b1f66beec81752b8c2ca0 to your computer and use it in GitHub Desktop.
Find vowels in JavaScript
//specs
vowels('Hello World!') //3
vowels('My name is javascript') //6
//solution 1
function vowels(str) {
const vowelChar = ['a', 'e', 'i', 'o', 'u'];
let count = 0;
for (let char of str.toLowerCase()) {
if (vowelChar.includes(char)) {
count++;
}
}
return count;
}
// solution 2
function vowels(str) {
const matches = str.match(/[aeiou]/gi)
return matches ? matches.length : 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment