Tell us what’s happening:
I don’t fully understand the third case, in which the first vowel comes in the end of the string.
translatePigLatin("glove") //should return "oveglay".
It’s confusing because it’s not returning the string backwards as I first thought. What exactly am I supposed to do if the case is true?
What it seems to be doing is removing each letter (from the start of the string) that isn’t a vowel until it finds one, in this case it removes “g” and “l” then finds “o” (a vowel). After that it takes those letters and add them to the end of the string along with the word “ay”
Am I correct here?, the challenge doesn’t mention any of that though and with my current code I’m failing the last case, not sure why.
Your code so far
function translatePigLatin(str) {
const vowels = ["a", "e", "i", "o", "u"]
for (let i = 0; i < vowels.length; i++) {
if(str.startsWith(vowels[i])) {
return str + "way"
} else if (str.endsWith(vowels[i])) {
return str
}
}
const arr = str.split("")
const firstLetter = arr.shift();
arr.push(firstLetter + "ay")
return arr.join("")
}
translatePigLatin("consonant");
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin