The Devowelizer

by kirupa   |   6 November 2013

Once you get familiar with loops, functions, arrays, and working with strings, you can put everything you learned into doing some pretty cool things. In this challenge, you will write a devowelizer. You may be wondering what a devowelizer actually is. Well, a devowelizer is a mostly tropical creature (ok...a function) that only eats the vowels of any text that is fed to it.

Let's say you have some text that looks as follows:

the word before 

After our devowelizer has gone through it, you will see something that looks as follows:

after the vowels have been removed 

Your goal is to build such a function. This function should take an arbitrary string as its argument, and it returns a new string with the vowels removed. Here is a very basic stub of what this could look like:

function devowelize(word) {
         var devowelizedWord = "";
        
         return devowelizedWord;
}

alert(devowelize("Kirupa Chinnathambi")); // display "Krpa Chnnthmb"

To simplify things a bit, don’t worry about optimizing for ridiculously large strings. Assume that the strings you want to devowelize will be at most ten sentences and one character at the very least.

Take about 15 minutes and try to create your own devowelizer. Once you have something (or are completely stuck), read on to see my proposed solution and some notes explaining why I did things the way I did.

Onwards!

 

 

 

 

 

 

( this space intentionally left blank...to keep you from accidentally seeing the solution )

 

 

 

 

 

 

 

 

My Solution

Here is what my implementation of the devowelizer looks like:

function devowelize(word) {
    var devowelizedWord = "";

    for (var i = 0; i < word.length; i++) {
        var character = word[i].toLowerCase();

        if (isVowel(character) == false) {
            devowelizedWord += character;
        }
    }
        
    return devowelizedWord;
}

alert(devowelize("Kirupa Chinnathambi"));

function isVowel(char) {
    return char == 'a' ||
           char == 'e' ||
           char == 'i' ||
           char == 'o' ||
           char == 'u' ||
           false;
}

Take a few moments to look over my code and see how I implemented it.

The basic approach is pretty simple. For any text that is passed in, I go through each character individually, and check if that character is a vowel or not:

for (var i = 0; i < word.length; i++) {
    var character = word[i].toLowerCase();

    if (isVowel(character) == false) {
        devowelizedWord += character;
    }
}

The actual vowel check is performed by the isVowel function:

function isVowel(char) {
    return char == 'a' ||
           char == 'e' ||
           char == 'i' ||
           char == 'o' ||
           char == 'u' ||
           false;
}

If the passed in character is a vowel, one of the checks will return true. If the passed in character is not a vowel, the isVowel function will return a false.

With this knowledge, if a character is a vowel, I don't do anything. If a character isn't a vowel, I add it to the devowelizedWord variable:

for (var i = 0; i < word.length; i++) {
    var character = word[i].toLowerCase();

    if (isVowel(character) == false) {
        devowelizedWord += character;
    }
}

The end result is that the devowelizedWord variable will contain a version of the original text without any of the vowels present.

Srsly? Incrementing a String?

From my solution, you may have noticed that I am not doing anything fancy here. All I am really doing is checking for a vowel and, if one doesn't exist, increment a string with the non-voweled character to construct the devoweled text. In JavaScript, just like many other languages, modifying a string doesn't result in the existing string object getting altered. There is no StringBuilder equivalent in this part of town. Instead, any string modification results in you seeing an entirely new string object that is created and contains the modified string value. That sounds disgusting, right?

Now, despite how negatively I described the situation, this solution turns out to be quite performant when compared to other approaches you can use such as joining arrays or using a regex function. There is a jsperf table that compares how long it takes to check for a vowel using several popular approaches as well as another table that shows the performance for concatenating strings.

Post a Better Solution

Do you have a better or different solution? If so, post in the Devowelizing Challenge thread and share your version.

If you have a question about this or any other topic, the easiest thing is to drop by our forums where a bunch of the friendliest people you'll ever run into will be happy to help you out!

THE KIRUPA NEWSLETTER

Get cool tips, tricks, selfies, and more...personally hand-delivered to your inbox!

( View past issues for an idea of what you've been missing out on all this time! )

WHAT DO YOU THINK?

NEWSLETTER

No spam. No fluff. Just awesome content sent straight to your inbox!

Awesome and high-performance web hosting!
BACK TO TOP
new books - yay!!!