Created
August 20, 2017 05:38
-
-
Save robwhitaker/03eb0af9b2ca1a5a2ed05d8bb840c1ae to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Write a function `hipsterfy(sentence)` that takes takes a string containing | |
// several words as input. Remove the last vowel from each word. `'y'` is not a vowel. | |
function isVowel(char) { | |
return ["a","e","i","o","u"].indexOf(char.toLowerCase()) !== -1; | |
} | |
function hipsterfy(str) { | |
return str.split("").reverse().reduce(function(acc, char) { | |
if(isVowel(char) && acc.needLastVowel) | |
return { needLastVowel: false, newString: acc.newString }; | |
else | |
return { needLastVowel: acc.needLastVowel || char === " " | |
, newString: char + acc.newString | |
}; | |
}, { needLastVowel: true, newString: ""}).newString; | |
} | |
console.log(hipsterfy("proper")); // => "propr" | |
console.log(hipsterfy("proper tonic panther")); // => "propr tonc panthr" | |
console.log(hipsterfy("towel flicker banana")); // => "towl flickr banan" | |
console.log(hipsterfy("runner anaconda")); // => "runnr anacond" | |
console.log(hipsterfy("turtle cheeseburger fries")); // => "turtl cheeseburgr fris" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment