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
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
Leave it to senocular for the most esoteric yet brilliant solution.
PS None of you remove the y from thymeā¦
I almost worked in a randomized-also-remove-y feature to handle the āsometimes yā rule, but didnāt want to overcomplicate things
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.
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
@REB_412 , @abergquist , @senocular , @krilnon , @steve.mills - you all have retroactively been granted the Devowelizer badge for having completed this exercise
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
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
Nicely done!