Pig Latin (Need Help)

function translatePigLatin(str) {

  // consonant letters: b, c, d, f, g, h, j, k, l, m, n, p, q, r, s, t, v, w, x, y, z
  // vowels: a, e, i, o, u,
  // if str starts with a consonant letter or two consecutive consonant letters...return the str with the consonant letters at the end + ay 
  // if str starts with a vowel return the str with the vowel at the end + way

let vowels = /"aeiou"/g;

for (let i = 0; i < str.length; i++) {
  if (str[0] == vowels) {
    str.push(str[0]+"ay")
  }
  return str;
} if (str[0] !== vowels) {
  str.push(str[0] && str[1] + "way")
}
return str; 
}

translatePigLatin("consonant");

I feel like I understand what is going on but I just can’t get to a solution. Can someone please help me out without giving me any answer?

  • what does this code do? can you please explain this?
  • what is type of “str”?

happy learning :slight_smile:

for (let i = 0; i < str.length; i++) {
  if (str[0] == vowels) {
    str.push(str[0]+"ay")
  }
  return str;

I thought this section would be a way of saying if the first letter in the word(str) is a vowel, then push that vowel + “ay” to the end of the word(str). Is my logic off?

  • where “push” method is allowed? or in other words is it allowed on “string” or “array”?

look at this example function call

translatePigLatin(“consonant”)

1 Like

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