Advertisement
Guest User

Untitled

a guest
Mar 9th, 2025
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const words = ["red", "yellow", "green", "blue", "purple", "radish", "rainbow"];
  2.  
  3. function removeVowels(words) {
  4.  
  5.   // map over each word in the words array.
  6.   // this is similar to what you did with the for loops.
  7.   // except .map() loops over a copy, rather than the original, to loop over the original use .forEach()
  8.   const filteredWords = words.map(
  9.     // Here we are taking each entry of the array (in this case: word) and checking against a regex, replacing each match with an empty string to remove it.
  10.     // let me explain the regex a bit.
  11.     // the slashes just indicate that its contents is a regex string,
  12.     // the [] means match any of the characters,
  13.     // g means it's global, or find all matches rather than stopping at the first one.
  14.     // i means it's case-insensitive.
  15.     word => word.replace(/[aeiou]/gi, "")
  16.   );
  17.  
  18.   // return the filtered array. You could place the words.map(...) code here, instead of filteredWords, but for clarity i decided not to.
  19.   return filteredWords;
  20. }
  21.  
  22. console.log(removeVowels(words));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement