logo

Vowel Count

Return the number (count) of vowels in the given string. We will consider a, e, i, o, u as vowels for this Kata (but not y). The input string will only consist of lower case letters and/or spaces.


My code:

function getCount(str) {
                        let arrayCount = str.split(''); 
                        let vowel = [];
                        for (let i = 0; i < arrayCount.length; i++) {
                          if (arrayCount[i] === "a" || arrayCount[i] === "e" || arrayCount[i] === "i" || arrayCount[i] === "o" || arrayCount[i] === "u") {
                            vowel.push(arrayCount[i]);
                                }
                            }
                        return vowel.length; 
                        }
                      
                    


And that's the best solution among all users:

function getCount(str) {
                        return (str.match(/[aeiou]/ig)||[]).length;
                      }
                    

Well, I've felt humbled. The best code solution is just one line. It's beautiful. I learned quite a bit from it, like the .match method and also the included flags /i and /g.