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.

Getting Help

If you have questions, need some assistance on this topic, or just want to chat - post in the comments below or drop by our friendly forums (where you have a lot more formatting options) and post your question. There are a lot of knowledgeable and witty people who would be happy to help you out

Share

Did you enjoy reading this and found it useful? If so, please share it with your friends:

If you didn't like it, I always like to hear how I can do better next time. Please feel free to contact me directly via e-mail, facebook, or twitter.

Brought to you by...

Kirupa Chinnathambi
I like to talk a lot - A WHOLE LOT. When I'm not talking, I've been known to write the occasional English word. You can learn more about me by going here.

Add Your Comment (or post on the Forums)

blog comments powered by Disqus

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