DEV Community

Javascript Algorithms #1: Counting the Vowels in a String Of Text

Philip Obosi on July 26, 2018

Over the years, Javascript has continued to gain lots of attention as its increasingly vast capabilities continue to expand. It has grown from b...
Collapse
 
boianivanov profile image
Boian Ivanov • Edited

A really good article for explaining algorithms through JavaScript for beginners. Good job and keep it up.

I thought though to try and add one more way of doing it, and that's recursively.
So here is my method for recursively finding the amount of vowels (feel free to use it).

const vowels = ["a", "e", "i", "o", "u"]

var count = 0;

function countVowelsRecursive(text) {
    if(vowels.includes(text[0].toLowerCase())) {
        count++;
    }
    if(text.length == 1){
        console.log(`The text contains ${count} vowel(s)`);
        return count;
    } else {
        countVowelsRecursive(text.substr(1));
    }
}

countVowelsRecursive('I am a world-class developer using iterations');
Enter fullscreen mode Exit fullscreen mode

The two important places are :

  • text[0] - gives us the first character of a string, because a string could be compared to an array of chars, and
  • text.substr(1) - removes the first character of the string, while it gives it to the method.

After those two, if we're left with only one character the method gives us the result and returns the counter.

Collapse
 
dimkl profile image
Dimitris Klouvas

It may be a correct algorithm but the implementation is not a very good one. The countVowelsRecursive should be a pure function and not depend to scoped count variable. What happens when we run the countVowelsRecursive twice in the same code snippet? Is the result of the 1st call the same as the 2nd?

Collapse
 
boianivanov profile image
Boian Ivanov

No worries, you can make it a function in a function with the parent one having the count = 0 and then the child one doing the iteration. Though you got the point that the idea behind it was to showcase different algorithms, right ?

Thread Thread
 
dimkl profile image
Dimitris Klouvas

Yeah, i mentioned that the algorithm is correct at the beginning. But i felt the urge to comment about the implementation because i think it matters. Nevermind, the topic is about the algorithm so let's move on.

Collapse
 
worldclassdev profile image
Philip Obosi

Awesome work Boain. Did you test the performance implications?

Collapse
 
boianivanov profile image
Boian Ivanov

I haven't tried, because I made it in like 10 minutes. But I presume it's could be close to yours. If you willing to test, do post the results here for future reference. I would be really interested to know.

Collapse
 
dallgoot profile image
dallgoot

i'm sorry : i've tried to run an alternate version of the first function and ended spamming tests :/
I'm also performance obsessed ;)

Collapse
 
worldclassdev profile image
Philip Obosi

Awesome Dallgoot. What's the issue with the tests?

Collapse
 
dallgoot profile image
dallgoot

No issues with the tests but many with the guy (me) who tried to make changes in them ;)
i wanted to try a version with a switch replacing the array check to see perf differences but failed miserably for some reason...

Collapse
 
dallgoot profile image
dallgoot

finally: I got some tests working (Revision 7, you can delete others)
So I'm seeing what I expected: switch cases are faster than array searching.
Not from much though, but still with so few values.

Thread Thread
 
worldclassdev profile image
Philip Obosi

Nice one man. Seen it.

Collapse
 
munamohamed94 profile image
Muna Mohamed

Great post Justine! Very detailed and well explained!

Collapse
 
worldclassdev profile image
Philip Obosi

Thanks alot Muna. That's the whole point of Series (Dev-process).

Collapse
 
zac11 profile image
Rahul Yadav

Looking forward to more such tutorials.

Collapse
 
worldclassdev profile image
Philip Obosi

Definitely Rahul... I'll keep sharing

Collapse
 
lakshmias profile image
Lakshminarayanan

is it ok to use javascript split ?
jsfiddle.net/qxd945eo/1/