DEV Community

NJOKU SAMSON EBERE
NJOKU SAMSON EBERE

Posted on • Updated 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 (4)

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()

The JavaScript Brief

1. Top 5 MERN STACK projects to improve your practical understanding

Boost your MERN Stack development skills by undertaking interesting beginner projects. These five engaging projects cover web applications and range from social media website applications to geo-social networking maps. Hone your understanding and apply modern techniques backed up by hands-on experience.

2. How To Optimize Your React App’s Performance

Learn the best optimizing techniques to make your React applications faster and more efficient. Focusing on the identification of performance bottlenecks and common pitfalls to avoid, these optimization strategies will keep your applications running smoothly even when faced with growing complexity.

3. A story of let, const, object mutation, and a bug in my code

In the pursuit of bug-free code, explore an incident involving a mix-up between const and let, making sure your custom code works effectively with third

party documentation. Discover best practices on program flow and learn about JavaScript's unpredictable aspects to ensure your core code is robust.