SHARE
TWEET

Untitled

a guest Jul 19th, 2019 69 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Write a script that will find all the vowels [a,e,i,o,u] inside any string
  3.  * and then flip the order of only the vowels. If there is an odd number of vowels
  4.  * then the one in the middle stays in the same position. Complete this task with the
  5.  * fewest number of loops and iterations as possible.
  6.  * Eg:
  7.  * cottage => cettago
  8.  * hello => holle
  9.  * sauce => seuca
  10.  * javascript => jivascrapt
  11.  */
  12. let log = console.log;
  13.  
  14. let process = word => {
  15.   let matches = Array.from(word).reduce((acc, curr, idx) => {
  16.     if (["a", "e", "i", "o", "u"].includes(curr)) {
  17.       acc.push(idx);
  18.     }
  19.     return acc;
  20.   }, []);
  21.   log(matches); //positions of the vowels
  22.   let len = matches.length;
  23.   let wordCopy = Array.from(word);
  24.   for (let i = 1; i <= len / 2; i++) {
  25.     let ltr = word.substring(matches[len - i], matches[len - i] + 1);
  26.     let removed = wordCopy.splice(matches[i - 1], 1, ltr);
  27.     wordCopy.splice(matches[len - 1], 1, removed[0]);
  28.     word = wordCopy.join("");
  29.   }
  30.   return word;
  31. };
  32.  
  33. log(process("javascript"));
  34. log(process("hello"));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top