Pig Latin issue

Tell us what’s happening:

I am trying to solve this pig latin challenge but my answer is not right. I tried to print return statements but console.log did not print anything.

Your code so far


function isVowel(c) {
    return ['a', 'e', 'i', 'o', 'u'].indexOf(c) !== -1
}

function translatePigLatin(str) {
  if(str[0].isVowel) {
    return str + "way";
  }else {
    for(var i = 0; i < str.length; i++) {
      if(str[i].isVowel) {
        return str.substring(0, i) + str.substring(i) + "ay";
      }
    }
  }
  
}

translatePigLatin("consonant");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:64.0) Gecko/20100101 Firefox/64.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/pig-latin/

Double check how you are using isVowel. It is a function not a method on str.

1 Like

You have created a function, so isVowel needs to be used as isVowel(str[0])

But even just changing that, I get consonant => consontay, but it works with words that starts with a vowel.

1 Like

Thank you. As you said, Except the way I use the function, there was an error for words that start with a consonant. I solved it by switching str.substring(0, i) and str.substring(i). But what should I do for words without vowels? I tried to return str without any changes but it didn’t work.

you would still need to move the consonant cluster to the end of the word and add ay, just that in this case the cluster is the whole word. So you just add “ay” at the end of the word

1 Like

Thank you so much. It worked.