Created
March 31, 2017 23:47
-
-
Save franvarney/732c7fd0d208c52cfd3c630350eed270 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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