Categories
Post

An ES6y Solution for Codewars’ Trolls

Spread the love
Description:

https://www.codewars.com/kata/52fba66badcd10859f00097e/train/javascript

Trolls are attacking your comment section!

A common way to deal with this situation is to remove all of the vowels from the trolls’ comments, neutralizing the threat.

Your task is to write a function that takes a string and return a new string with all vowels removed.

For example, the string “This website is for losers LOL!” would become “Ths wbst s fr lsrs LL!”.

Note: for this kata y isn’t considered a vowel.

The code:

disemvowel=s=>[...s].filter(c=>!"aeiouAEIOU".includes(c)).join('')

It could be made shorter by using regular expressions but regex is its own meta-language.

It could argued that it would be made more clear by doing only one operation per line and using traditional functions declaration and for loops, but I am siding with the modern vs the traditional here:

e.g.

function disemvowel(s){
  output = "";
  for(i=0;i<s.length;i++){
    if(!"aeiouAEIOU".includes(s[i])){
      output += s[i];    
    }
  }
  return output;
}

Links to MDN articles discussing the techniques used here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.