I was doing the Pig Latin exercise and I entered in the following code:
function translatePigLatin(str) {
const consonants = /^[^aeiou]*/
if (consonants.test(str)) {
const info = str.match(consonants)
const fragment = str.replace(info[0], "")
const newStr = fragment + info[0] + "ay"
return newStr
}
return str + "way"
}
The code works just fine with words that start with consonants, but not with words that start with vowels. When I put the words “algorithm” and “eight” as parameters in the function, I get “algorithmay” and “eightay” as results instead of the word with “way” at the end like it should be (“algorithmway” and “eightway”). I also tried returning the string with any word added on to the end but still got the word ending with “ay”. Can anyone help?