Skip to content

Instantly share code, notes, and snippets.

@franvarney
Created March 31, 2017 23:47
Show Gist options
  • Save franvarney/732c7fd0d208c52cfd3c630350eed270 to your computer and use it in GitHub Desktop.
Save franvarney/732c7fd0d208c52cfd3c630350eed270 to your computer and use it in GitHub Desktop.
//reverseVowels
//"whiteboard"
//"whatobeird"
const VOWELS = ['a','e','i','o','u'];
function isVowel(char) {
return VOWELS.indexOf(char) >= 0;
}
function reverseVowels(string) {
var half = Math.floor(string.length / 2);
var i = 0, j = string.length - 1;
while (i < half) {
let a = string[i];
let b = string[j];
if (isVowel(a) && isVowel(b)) {
string = string.substr(0, i) + b + string.substr(i + 1);
string = string.substr(0, j) + a + string.substr(j + 1);
++i;
--j;
}
if (!isVowel(a)) ++i;
if (!isVowel(b)) --j;
}
return string;
}
console.log(reverseVowels('whiteboard'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment