Issue with Pig Latin exercise

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?

Do you understand what the * does here?

@camperextraordinaire Yes. It’s supposed to find zero or more instances of a letter that’s not a vowel

@camperextraordinaire Oh I figured it out now. Since the * means “zero or more instances”, the regular expression would return true whether the word starts with a consonant or not. Thank you

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.