The Devowelizer Coding Challenge

This is a companion discussion topic for the original entry at http://www.kirupa.com/html5/the_devowelizer.htm

Something new I want to try outside of the usual deconstructions I provide. How about simple challenges that test one’s understanding of certain concepts. The first one is what I describe as The Devowelizer. Basically, this function removes any and all vowels from a string and returns the devowelized version.

Here is the solution I described in this article:

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;
}

Do any of you have an alternate solution you would propose?

Cheers!
Kirupa :cyclone:

I would use the following:

function devowelize(word) {
	let testPattern = /a|e|i|o|u|/gi;
	return word.replace(testPattern,'');
}

Also in the above code should not the line
var character = word*
be
var character = word[i]

you’re missing isVowel (unless maybe I missed it before because it was being defined before the call (yay hoisting!) and you had to scroll to see it) and something is up with word* in your loop

Edit: @kirupa I fixed your original code (re: word*) based on the code in the link

For the vowel list, you can use a character set: /[aeiou]/gi

If you didn’t know, strings are iterables and can be spread with the spread (...) syntax.

console.log([..."Kirupa Chinnathambi"])
// ["K", "i", "r", "u", "p", "a", " ", "C", "h", "i", "n", "n", "a", "t", "h", "a", "m", "b", "i"]

What if we overrode that behavior with a devowelizer?

String.prototype[Symbol.iterator] = function * () {
  const vowels = 'aeiou'
  for (const char of this.split('')) {
    if (!vowels.includes(char.toLowerCase())) {
      yield char
    }
  }
}
console.log([..."Kirupa Chinnathambi"])
// ["K", "r", "p", " ", "C", "h", "n", "n", "t", "h", "m", "b"]

:-o

2 Likes

Leave it to senocular for the most esoteric yet brilliant solution.

PS None of you remove the y from thyme…

1 Like

I almost worked in a randomized-also-remove-y feature to handle the “sometimes y” rule, but didn’t want to overcomplicate things :wink:

Thank you for the clarification. With it, the function reduces to just a single line:
return word.replace(/[aeiou]/gi,’’)

Thanks for noticing the bug in my code @REB_412 and @senocular! Not sure what happened there - must have been a weird copy/paste issue?

It’s funny (to me, at least!) for how this thread (from 2013) got bumped up today. I was fixing some broken links, stumbled upon my original post, and noticed that the :cyclops: smiley from 2013 no longer worked. I edited the post with the closest smiley that started with :cy and ended up with the cyclone. That edit bumped this thread to the top and you all did some great things with it since haha.

My minimal-effort Swift version, without trying to be efficient:

extension String {
    var devowelized: Self {
        filter { !"aeiou".contains($0.lowercased()) }
    }
}

"the quick brown fox jumps over the lazy dog.".devowelized
// th qck brwn fx jmps vr th lzy dg.

Turns out that Unicode doesn’t have anything special to say about vowels, so there’s nothing in Unicode.Scalar.Properties to help avoid manually writing out the list of English vowels, or generalize over other languages if they have the concept of vowels.

2 Likes

Since the code issue has been solved, here’s something you can share at your next cocktail party:

  • “abstemious” and “facetious” are the only 2 words in the English language that contain all 5 vowels appearing once and in order
  • if “y” is also considered a vowel, then “abstemiously” and “facetiously” are the only 2 words in the English language that contain all 6 vowels appearing once and in order
2 Likes