Finding vowels algorithm in JavaScript.

Isaac Avilez
2 min readDec 24, 2020

--

In this blog I will be writing about the vowel algorithm which asks that we return the number of vowels used in a string. Simple, right? Well, let’s get to it! Let’s start off by always writing out our function declaration, and we will have one parameter, that being the string. After we’ve done so, we want to have a counter, so we’ll set a variable named counter to 0. So far our code should be looking like this:

Function declaration.

After that, we want to put all of our vowels within an array, so that when we’re checking through each character of the string, we will be able to utilize the .includes() method to check for any vowels. After we’ve created a variable named vowels for each of vowel in an array as their own element, we then want to start a for loop over our given “str” argument. We want our string to all be lower case so we will do str.toLowerCase() so our for loop will look like for(let char of str.toLowerCase()) because our vowels array is all in lowercase. With that, our code should look similar to this:

For loop with vowels array.

Now, within our for loop, we want to make a conditional. Our conditional will check for if our char includes any of the given vowels within our vowels array. If it includes a vowel, we want to increment our counter. After we’ve done so, outside of our for loop, we want to return our counter variable. With that being done, our code is going to look like this:

Finalized vowels algorithm.

I hope you enjoyed this simple algorithm for our vowel checker in JavaScript! Hopefully this will help you get a better understanding of JavaScript!

--

--

No responses yet