Algorithms and Data Structures Part X

Brian Butterly
3 min readApr 16, 2021

Vowels

This week, we have a pretty easy problem. We are asked to write a function that returns the number of vowels used in a string. Vowels are a,e,i,o and u.

That’s it, that’s all we have to do.

Okay so here we go, we start with our empty function…

taking in an argument of a string. Now we need to create a counter variable…

then we need to iterate through the string…

I added .toLowerCase to this to ensure that capital letters are included. Now the str will be all lower case letters and any vowels that where capital will be included in our iteration.

Now we need some logic in here to decide if we’re working with a vowel. So, how do we check to see if we’re working with a vowel? well the most straightforward way to handle this is with a helper method that’s included with all strings, and arrays for that matter. This method is called .includes(). This will take in a sub string and return a boolean answer…

E.G.

const word = "hello world"word.includes("e") -> True
word.includes("ello") -> True
word.includes("m") -> False

so using this in our problem will look like this…

first we need to create an array, not a string. a string will work but we want to make our code as clear as possible to any other developer that may be looking at it. Using an array just makes it all a little more clear…

so now that we have our array (name it whatever you want), we will move onto our if statement…

so if checker includes whatever char we’re looking at, then increment the counter. Finally then we need to return count…

and that’s it! below is the full code.

Thanks for reading, see ya next week!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Brian Butterly
Brian Butterly

Written by Brian Butterly

Learning to code, one day at a time.

No responses yet

Write a response