Skip to content

Instantly share code, notes, and snippets.

@eldyvoon
Created January 5, 2018 03:29
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