Skip to content

Instantly share code, notes, and snippets.

@CodeDotJS
Last active February 7, 2019 10:06
Show Gist options
  • Save CodeDotJS/ce3f7491bd0b6c6c81dc70deb0cb5810 to your computer and use it in GitHub Desktop.
Save CodeDotJS/ce3f7491bd0b6c6c81dc70deb0cb5810 to your computer and use it in GitHub Desktop.
Function that takes a string as input and reverses only the vowels of a string
'use strict';
const reverseVowelString = s => {
let str = s.split('');
const voewls = ['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'];
const storeVowelsFromString = [];
for (let i = 0; i <= str.length - 1; i++) {
for (let j = 0; j <= voewls.length - 1; j++) {
if (str[i] === voewls[j]) {
storeVowelsFromString.push(str[i]);
str[i] = '_';
}
}
}
const revStr = storeVowelsFromString.reverse();
let index = 0;
return str.map(a => a === "_" ? revStr[index++] : a).join('');
};
console.log(reverseVowelString('ENTREPRENEURIAL'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment