Start learning free

LESSON 216 OF 285 · LOOPS WITH STRINGS, ARRAYS & DICTIONARIES

Count the Vowels in JavaScript: for...of and includes

Practice Read the full explanation

Given a string s, return how many vowels it has.

The vowels are a, e, i, o and u, in lower case or in upper case. The letter y is not a vowel here. Every other character (consonants, spaces, digits, punctuation) is skipped.

 

Example 1:

Input: s = "Hello World"
Output: 3
Explanation: The vowels are e, o and o. The other characters, the space included, are skipped.

Example 2:

Input: s = "AlgoCademy"
Output: 4
Explanation: A, o, a and e are vowels: the upper-case A counts too. The y doesn't count.

Example 3:

Input: s = "Why try?"
Output: 0
Explanation: y is not a vowel, so this string has none.

 

Constraints:

  • s has at least one character and doesn't start or end with a space.
  • s contains English letters, digits, spaces and punctuation.
STEP 1 OF 5
    Ctrl + Enter to check

    JAVASCRIPT · LOOPS WITH STRINGS, ARRAYS & DICTIONARIES

    Count The Vowels in JavaScript, explained

    Problem overview

    The method countVowels receives a string s and returns how many vowels it contains. The vowels are a, e, i, o and u, in lower case or upper case. The letter y is not a vowel here. Everything else, whether consonants, spaces, digits or punctuation, is skipped.

    In "Hello World" the vowels are e, o and o, so the answer is 3. In "AlgoCademy" they are A, o, a and e, so the answer is 4: the upper-case A counts and the y doesn't. "Why try?" has none, so the answer is 0.

    Approach: look at each character and ask a yes or no question

    The method has to examine every character once and keep a tally. For each character the question is the same: is it one of the ten vowel characters? A string holding exactly those ten characters, "aeiouAEIOU", works as the list of allowed values. Both cases are written out, so no case conversion is needed.

    The question "does this string contain this character?" is what the string method includes answers: "aeiouAEIOU".includes("e") is true, and "aeiouAEIOU".includes("y") is false.

    Step by step

    1. Store the vowels in a const named vowels: "aeiouAEIOU". It never changes, so const says so. The letter y is left out on purpose.
    2. Declare count with let, starting at 0. It changes inside the loop.
    3. Loop over the string with for (let c of s). A for...of loop over a string visits each character in order, and c holds the current one as a one-character string.
    4. Test the character. If vowels.includes(c) is true, add 1 to count with ++. Consonants, spaces, digits and punctuation aren't in vowels, so they change nothing.
    5. Return count after the loop. Returning inside the loop would stop at the first character.

    The solution in JavaScript

    class Solution {
        countVowels(s) {
            const vowels = "aeiouAEIOU";
            let count = 0;
            for (let c of s) {
                if (vowels.includes(c)) {
                    count++;
                }
            }
            return count;
        }
    }
    

    Details worth knowing:

    • for...of against for...in. for (let c of s) gives the characters themselves. for (let i in s) would give the positions as strings, "0", "1", and so on, which are not what the check needs.
    • There is no separate character type. A character is just a string of length 1, so c can be passed straight to includes.
    • includes looks for a substring. That is fine here, since c is a single character. It also means that "aeiouAEIOU".includes("") and "aeiouAEIOU".includes("ae") are both true, so the method must never pass anything longer than one character.
    • The check is case-sensitive. "E" is a different character from "e", which is why both cases are in vowels. Another option is to lower-case first with s.toLowerCase() and keep only five letters in the string.

    Edge cases and common mistakes

    • No vowels. "Why try?" returns 0, and so does "xyz".
    • Only vowels. "AEIOU aeiou" returns 10: the space is skipped and each of the ten letters counts.
    • Forgetting upper case. A vowels string of only "aeiou" would miss the capital A in "AlgoCademy" and return 3 instead of 4.
    • Counting y. Adding it to the vowel list changes the answers for words like "Why try?", which would give 2 instead of 0.
    • Returning too early. A return inside the loop ends the method at the first character.
    • Counting distinct vowels. The task counts every occurrence. "Hello World" has two o's, and both count.

    Testing

    • s = "Hello World" gives 3.
    • s = "AlgoCademy" gives 4.
    • s = "Why try?" gives 0.
    • s = "xyz" gives 0.
    • s = "AEIOU aeiou" gives 10.
    Keep going with JavaScript1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free