The Devowelizer Coding Exercise

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

@REB_412 , @abergquist , @senocular , @krilnon , @steve.mills - you all have retroactively been granted the Devowelizer badge for having completed this exercise :stuck_out_tongue:

Iā€™m nearly done converting all old exercises into badge-able activities!

Just had to go with the regexā€¦

TheDEVOWELIZER = function(TAKEMYVOWELS) {
  gsub("[aeiou]", "", TAKEMYVOWELS, ignore.case = T)
}

TheDEVOWELIZER("Kirupa Chinnathambi jumps over the lazy dog.")

Hereā€™s another oneā€¦

  let devowelize = (str) => [...str].filter(letter => !'iouaešŸ˜œ'.includes(letter.toLowerCase())).join('');
  console.log(devowelize("Kirupa ChinnathambišŸ˜œ"));
> Krp Chnnthmb

One of the advantages of this method is as you can see here it can handle unicode stuff thanks to the [ā€¦str] and includes. So you can use it to strip all my stupid smiley faces that I use to often :stuck_out_tongue_winking_eye:

And if we try the same thing with the old codeā€¦

  function devowelizer(word) {
    let devowelizedWord = "";

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

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

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


  console.log(devowelizer("Kirupa ChinnathašŸ˜œmbi"));
> krp chnnthšŸ˜œmb
2 Likes

Nicely done! :slight_smile: