Count all vowels in string For a homework assignment, I was asked to complete this CodeStepByStep problem. It's coded correctly and gave me a right answer. I'm just wondering if there's an easier way to solve this problem without the big blocky code in the if statement. The fact that I have to check each lowercase vowel AND THEN the uppercase vowel seems verbose.

Write a function named vowelCount that accepts a string and returns
the number of vowels (a, e, i, o, or u) that the string contains.
For example, the call of vowelCount(&quot;kookaburra&quot;) should return 5 (two
o's, 2 a's, and one u). When passed a string without any vowels (such
as an empty string, &quot;01234&quot;, or &quot;sky&quot;), 0 should be returned.

function vowelCount(string) {
    if(string.length == 0) { return 0; }
    let count = 0;
    for(let i = 0; i &lt; string.length; i++) {
        let v = string[i];
        if(v == &quot;a&quot; || v == &quot;e&quot; || v == &quot;i&quot; || v == &quot;o&quot; || v == &quot;u&quot; ||
           v == &quot;A&quot; || v == &quot;E&quot; || v == &quot;I&quot; || v == &quot;O&quot; || v == &quot;U&quot;) {
            count += 1;
        }
    }
    return count;
}
An alternative route is to use string.replace() and Regular Expressions to strip everything but the vowels from the string. Then count the length of the resulting string. This avoids iteration altogether.



const vowelCount = s =&gt; s.replace(/[^aeiou]/gi, '').length

console.log(vowelCount('The quick brown fox jumps over the lazy dog'))




The regex is /[^aeiou]/gi which means match everything that's NOT (^) in the set (aeiou), matching globally (g flag) and without regard to case (i flag). string.replace() then uses this pattern to replace all matching characters with a blank string. What remains are your vowels.