The KIRUPA orange logo! A stylized orange made to look like a glass of orange juice!

FORUMS

The Devowelizer

by kirupa   |    filed under JavaScript Tricks

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:

 

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

 

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) {
  let devowelizedWord = "";

  return devowelizedWord;
}

alert(devowelize("Kirupa Chinnathambi")); // display "Krp 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, some notes explaining why I did things the way I did, and a link to some other solutions your peers may have provided as well.

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) {
  let devowelizedWord = "";

  for (let i = 0; i < word.length; i++) {
    let 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...um, basic. For any text that is passed in, I go through each character individually, and check if that character is a vowel or not:

for (let i = 0; i < word.length; i++) {
  let 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 (let i = 0; i < word.length; i++) {
    let 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.

Share and Learn!

Do you have a better or different solution? If so, post in the Devowelizing Challenge thread and share your version. Take a look at solutions others have posted as well.

Got a question or just want to chat? Comment below or drop by our forums (they are actually the same thing!) where a bunch of the friendliest people you'll ever run into will be happy to help you out!

When Kirupa isn’t busy writing about himself in 3rd person, he is practicing social distancing…even on his Twitter, Facebook, and LinkedIn profiles.

Hit Subscribe to get cool tips, tricks, selfies, and more personally hand-delivered to your inbox.

COMMENTS

Serving you freshly baked content since 1998!
Killer hosting by (mt) mediatemple

Twitter Youtube Facebook Pinterest Instagram Github