DEV Community

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Reverse - Vowels

/**
 * @param {string} s
 * @return {string}
 */
var reverseVowels = function (s) {
  const vowels = "aeiouAEIOU";
  const strArray = s?.split("");

  let vowelsInString = [];

  for (let i = 0; i < strArray.length; i++) {
    if (vowels.includes(strArray[i])) {
      vowelsInString.push(strArray[i]);
    }
  }

  vowelsInString = vowelsInString.reverse();
  for (let i = 0; i < strArray.length; i++) {
    if (vowels.includes(strArray[i])) {
      strArray[i] = vowelsInString.shift();
    }
  }
  return strArray?.join("");
};

reverseVowels("aA");

Enter fullscreen mode Exit fullscreen mode

Top comments (0)