## DEV Community is a community of 698,016 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

# Algorithm 101: 13 Ways to Count Vowels in a String NJOKU SAMSON EBERE
I love providing solutions to real world problems. When I am not coding, I enjoy movies, hanging out and working out.

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

``````

### 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;
}
``````
• 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;
}
``````
• 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;
}
``````
• 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;
}
``````
• 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;
}
``````
• 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;
}
``````
• 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;
}
``````
• 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;
}
``````
• 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;
}
``````
• 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;
}
``````
• 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;
}
``````
• 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;
}
``````
• regEx and if...statement
``````      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.

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

## Discussion (4) JSmith AR • Edited

another simple solution

``````const vowelsCounter_ = str => str.split('').filter(ch => 'aeiouAEIOU'.includes(ch)).length
`````` NJOKU SAMSON EBERE

This is lovely too. Using split() and filter() JSmith AR • Edited

I love the simple and shorts solutions.

``````const vowelsCounter = str => str.replace(/[^aeiou]/ig, '').length
vowelsCounter('njoku samson ebere')
`````` NJOKU SAMSON EBERE

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