In the last section we went through an iterative solution for the Vowles function.
We're now going to replace this with a slightly more condensed version.
So I'm going to copy this thing down to the bottom of the file.
As usual.
We'll comment it out just to leave a backup here, and then I'm going to clean out the contents of the
vowels function.
Now this alternative solution is going to make use of a regex expression or a regular expression.
So using a regular expression here, in my opinion, is so easy it's almost like cheating.
So what we're going to do is to look at the string we're going to call the match method on the string
matches used to see if some possible thing right here is included inside of this string.
So we can pass in a regular expression.
And this regular expression is going to check to see if we have any vowels inside of here.
So our regular expression is going to have a pair of square braces.
We put in a square pair of square braces like this.
We're essentially saying, hey, if this string contains any character that is inside of the square
brackets right here, then let us know.
So we're going to put in all the characters that we care about a e I owe you.
The other thing that we're going to do is to add on two options to the Reg X itself, both G and I,
the g regular expression right here.
Make sure that we don't stop at the first match that we find inside of our string.
So if there are multiple vowels of some type in there, we're going to attempt to find them all.
And then the I right here stands for insensitive or case insensitive.
So remember before we had to manually turn our string into a lowercase version of itself.
The I right here will automatically take care of cases for us.
Now, just one little odd thing about this match function right here.
If it finds any matches, it will return an array of all the matches that were found.
So we will return that and assign it to a variable called matches.
But the part that's kind of awkward is that if no matches are found, then match will return null instead.
So in other words, matches right here will be either an array or null.
So we need to handle both cases appropriately.
We're going to use a ternary expression for this.
So we're going to look at the matches variable.
If matches is null, null is considered to be a false value.
If it is an array, well then that is considered to be a true value.
So for the first part of our ternary expression, we'll say if this thing is a tricky value.
So if it is in an array, then I want to return matches length.
Otherwise if matches is null or if it is a false value, then I want to produce zero.
And then ultimately we really want to just return the results of the ternary expression.
So we'll make sure we put the return keyword in there as well.
All right.
So we'll save this and let's see how we're doing now.
So still for passing.
Fantastic.
Again, this is definitely more concise, but it really is kind of predicated on you really closely
understanding exactly how match works, especially the fact that it returns null if no matches are found.
Personally, if I were you, I would probably stray towards this iterative solution.
It's very clear and you can have some side discussions with your interviewer, like the one that I just
mentioned at the end of the last video where using an array here is kind of the checker thing rather
than a string is beneficial.
So really up to you.
But honestly, I think that the kind of value or the iterative solution down here is probably the safer
bet.
So this has been a nice little breather, nice little easy one.
Let's now continue with our next exercise in the next section.