DEV Community

Cover image for Codewars - Disemvowel Trolls
Nicolás Bost
Nicolás Bost

Posted on

Codewars - Disemvowel Trolls

Fun way to start a coding experience.

Oh no. Trolls took the comment section by storm. We need to do something. Hiring mods requires money, apparently, so instead we are tasked with altering the comment. Why? No idea, just do it:

function disemvowel(str) {
  // your code here
  return str;
}
Enter fullscreen mode Exit fullscreen mode

But what code?

Dain bramage

Becoming friends with documentation is essential to avoid double homework. MDN to the rescue. The function alters the string passed in. This is the result:

function disemvowel(str) {
  str = str.replaceAll(/[aeiouAEIOU]/, "");
  return str;
}
Enter fullscreen mode Exit fullscreen mode

Wait. It doesn't work. But why? The syntax is correct, right?
Kinda. It's missing one key element. The g flag.

What is that and why it exists? I would like to know as well. Unfortunately, all I can say is experts implemented it this way, and it works.

Corrected:

function disemvowel(str) {
  str = str.replaceAll(/[aeiouAEIOU]/g, "");
  return str;
}
Enter fullscreen mode Exit fullscreen mode

Not the best, nor the simplest solution. Would like to know how to benchmark code the right way. But that's for another time.

'Til next time. Drink water 💧💧💧.

Top comments (0)