From 14cc7d40bdad09c01fc0441963dd87186f426e59 Mon Sep 17 00:00:00 2001 From: Cody Loyd Date: Fri, 15 Dec 2017 12:52:22 -0600 Subject: [PATCH] piglatin --- pig_latin/pigLatin.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/pig_latin/pigLatin.js b/pig_latin/pigLatin.js index c9784c3..e483071 100644 --- a/pig_latin/pigLatin.js +++ b/pig_latin/pigLatin.js @@ -1,9 +1,23 @@ -function translate() { - // body... +function translate(string) { + return string + .split(" ") + .map(word => { + const index = firstVowelIndex(word); + const beginning = word.slice(0, index); + const ending = word.slice(index); + return `${ending}${beginning}ay`; + }) + .join(" "); } +function firstVowelIndex(string) { + const vowels = string.match(/[aeiou]/g); + if (vowels[0] == "u" && string[string.indexOf(vowels[0]) - 1] == "q") { + return string.indexOf(vowels[1]); + } + return string.indexOf(vowels[0]); +} module.exports = { - translate -} - + translate +};