Created
January 5, 2018 03:29
-
-
Save eldyvoon/35f2d756953b1f66beec81752b8c2ca0 to your computer and use it in GitHub Desktop.
Find vowels in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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