Algorithm 101: 13 Ways to Count Vowels in a String
Tags: algorithms, javascript, tutorial, string
Published: 2020-03-03 11:54:16

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

```javascript

vowelsCounter('njoku'); // 2

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

```

### 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

```javascript
      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;
      }
```

* split() method, includes method, for...of loop and if...statement

```javascript
      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;
      }
```

* spread operator, includes method, for...of loop and if...statement

```javascript
      function vowelsCounter(text) {
        let counter = 0;
        let vowels = ["a", "e", "i", "o", "u"];

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

        return counter;
      }
```

* spread operator, for...of loop and if...statement

```javascript
      function vowelsCounter(text) {
        let counter = 0;

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

        return counter;
      }
```

* split() method, for...in loop and if...statement

```javascript
      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;
      }
```

* split() method, includes() method, for...in loop and if...statement

```javascript
      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;
      }
```

* spread operator, includes() method, for...in loop and if...statement

```javascript
      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;
      }
```

* spread operator, for...in loop and if...statement

```javascript
      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;
      }
```

* spread operator, for...loop and if...statement

```javascript
      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;
      }
```

* spread operator, includes() method, for...loop and if...statement

```javascript
      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;
      }
```

* split() method, for... loop and if...statement

```javascript
      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;
      }
```

* split() method, includes() method, for... loop and if...statement

```javascript
      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;
      }
```

* regEx and if...statement

```javascript
      function vowelsCounter(text) {
        let regEx = /[aeiou]/gi;
        let matchedVowels = text.match(regEx);

        if (matchedVowels) {
          return matchedVowels.length;
        }

        return 0;
      }
```

### 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](https://dev.to/ebereplenty/algorithm-101-6-ways-to-find-the-most-recurring-character-in-a-string-5036)

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

**[Twitter](https://twitter.com/eberetwit) | [LinkedIn](https://www.linkedin.com/in/samson-ebere-njoku-profile/) | [Github](https://github.com/EBEREGIT)**

Thank You For Your Time.