Find The Vowels


Directions

  • Write a function that returns the number of vowels used in a string
  • Vowels are the characters 'a', 'e', 'i', 'o', and 'u'.
  • examples:
vowels('Hi There!') //--> 3
vowels('Why do you ask?') //--> 4
vowels('Why?') //--> 0

Solutions

JS

My Solution

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

SG Solution 1

function vowels(str) {
    const matches = str.match(/[aeiou]/gi);
    return matches ? matches.length : 0;
}

SG Solution 2

function vowels(str) {
    let count = 0;
    const checker = ['a', 'e', 'i', 'o', 'u'];

    for (let char of str.toLowerCase()) {
        if (checker.includes(char)) {
            count++;
        }
    }

    return count;
}
Made with Gatsby G Logo