DEV Community

NJOKU SAMSON EBERE
NJOKU SAMSON EBERE

Posted on • Edited on

Algorithm 101: 13 Ways to Count Vowels in a String

In this episode of Algorithm 101, we will be looking at thirteen (13) different ways to count the vowels in a given string.


vowelsCounter('njoku'); // 2

vowelsCounter('njoku samson ebere'); // 7

Enter fullscreen mode Exit fullscreen mode

Prerequisite

This article assumes that you have basic understanding of javascript's string methods and array methods.

Let's count the vowels in a given string using:

  • split() method, for...of loop and if...statement
      function vowelsCounter(text) {
        let splittedText = text.toLowerCase().split("");
        let counter = 0;

        for (char of splittedText) {
          if (
            char === "a" ||
            char === "e" ||
            char === "i" ||
            char === "o" ||
            char === "u"
          ) {
            counter++;
          }
        }

        return counter;
      }
Enter fullscreen mode Exit fullscreen mode
  • split() method, includes method, for...of loop and if...statement
      function vowelsCounter(text) {
        let splittedText = text.toLowerCase().split("");
        let counter = 0;
        let vowels = ["a", "e", "i", "o", "u"];

        for (char of splittedText) {
          if (vowels.includes(char)) {
            counter++;
          }
        }

        return counter;
      }
Enter fullscreen mode Exit fullscreen mode
  • spread operator, includes method, for...of loop and if...statement
      function vowelsCounter(text) {
        let counter = 0;
        let vowels = ["a", "e", "i", "o", "u"];

        for (char of [...text]) {
          if (vowels.includes(char)) {
            counter++;
          }
        }

        return counter;
      }
Enter fullscreen mode Exit fullscreen mode
  • spread operator, for...of loop and if...statement
      function vowelsCounter(text) {
        let counter = 0;

        for (char of [...text]) {
          if (
            char === "a" ||
            char === "e" ||
            char === "i" ||
            char === "o" ||
            char === "u"
          ) {
            counter++;
          }
        }

        return counter;
      }
Enter fullscreen mode Exit fullscreen mode
  • split() method, for...in loop and if...statement
      function vowelsCounter(text) {
        let splittedText = text.toLowerCase().split("");
        let counter = 0;

        for (char in splittedText) {
          if (
            splittedText[char] === "a" ||
            splittedText[char] === "e" ||
            splittedText[char] === "i" ||
            splittedText[char] === "o" ||
            splittedText[char] === "u"
          ) {
            counter++;
          }
        }

        return counter;
      }
Enter fullscreen mode Exit fullscreen mode
  • split() method, includes() method, for...in loop and if...statement
      function vowelsCounter(text) {
        let splittedText = text.toLowerCase().split("");
        let counter = 0;
        let vowels = ["a", "e", "i", "o", "u"];

        for (char in splittedText) {
          if (vowels.includes(splittedText[char])) {
            counter++;
          }
        }

        return counter;
      }
Enter fullscreen mode Exit fullscreen mode
  • spread operator, includes() method, for...in loop and if...statement
      function vowelsCounter(text) {
        let counter = 0;
        let vowels = ["a", "e", "i", "o", "u"];

        for (char in [...text]) {
          if (vowels.includes(text[char])) {
            counter++;
          }
        }

        return counter;
      }
Enter fullscreen mode Exit fullscreen mode
  • spread operator, for...in loop and if...statement
      function vowelsCounter(text) {
        let counter = 0;

        for (char in [...text]) {
          if (
            text[char] === "a" ||
            text[char] === "e" ||
            text[char] === "i" ||
            text[char] === "o" ||
            text[char] === "u"
          ) {
            counter++;
          }
        }

        return counter;
      }
Enter fullscreen mode Exit fullscreen mode
  • spread operator, for...loop and if...statement
      function vowelsCounter(text) {
        let counter = 0;

        for (let i = 0; i <= [...text].length; i++) {
          if (
            text[i] === "a" ||
            text[i] === "e" ||
            text[i] === "i" ||
            text[i] === "o" ||
            text[i] === "u"
          ) {
            counter++;
          }
        }

        return counter;
      }
Enter fullscreen mode Exit fullscreen mode
  • spread operator, includes() method, for...loop and if...statement
      function vowelsCounter(text) {
        let counter = 0;
        let vowels = ["a", "e", "i", "o", "u"];

        for (let i = 0; i <= [...text].length; i++) {
          if (vowels.includes(text[i])) {
            counter++;
          }
        }

        return counter;
      }
Enter fullscreen mode Exit fullscreen mode
  • split() method, for... loop and if...statement
      function vowelsCounter(text) {
        let splittedText = text.toLowerCase().split("");
        let counter = 0;

        for (let i = 0; i <= splittedText.length; i++) {
          if (
            splittedText[i] === "a" ||
            splittedText[i] === "e" ||
            splittedText[i] === "i" ||
            splittedText[i] === "o" ||
            splittedText[i] === "u"
          ) {
            counter++;
          }
        }

        return counter;
      }
Enter fullscreen mode Exit fullscreen mode
  • split() method, includes() method, for... loop and if...statement
      function vowelsCounter(text) {
        let splittedText = text.toLowerCase().split("");
        let counter = 0;
        let vowels = ["a", "e", "i", "o", "u"];

        for (let i = 0; i <= splittedText.length; i++) {
          if (vowels.includes(splittedText[i])) {
            counter++;
          }
        }

        return counter;
      }
Enter fullscreen mode Exit fullscreen mode
  • regEx and if...statement
      function vowelsCounter(text) {
        let regEx = /[aeiou]/gi;
        let matchedVowels = text.match(regEx);

        if (matchedVowels) {
          return matchedVowels.length;
        }

        return 0;
      }
Enter fullscreen mode Exit fullscreen mode

Conclusion

There are many ways to solve problems programmatically. You are only limited by your imagination. I will love to know other ways you solved yours in the comment section.

If you have questions, comments or suggestions, please drop them in the comment section.

Up Next: Algorithm 101: 6 Ways to Find the Most Recurring Character in a String

You can also follow and message me on social media platforms.

Twitter | LinkedIn | Github

Thank You For Your Time.

Top comments (2)

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

Wow... using replace() and regEx. It's really short and straight to the point

Collapse
 
ebereplenty profile image
NJOKU SAMSON EBERE

This is lovely too. Using split() and filter()