Pig Latin - Help me make my code work

I know this is a poor code but this is what I could come up with. This code passed all tests except the third one (“glove”). Please, don’t provide me a totally different code or an easier one, help me to change my current code.

function translatePigLatin(str) {
  var str1 = str;
  var str2 = str1.charAt(0);
  var str2_1 = str1.charAt(1);
  if (str2 == 'a' || str2 == 'e' || str2 == 'i' || str2 == 'o' || str2 == 'u') {
    var str3 = str;
    var str4 = str3.split('');
    var str5 = str4.push('way');
    var str6 = str4.join('');
    return str6;
  } else if (str2 !== 'a' || str2 !== 'e' || str2 !== 'i' || str2 !== 'o' || str2 !== 'u') {
    var str7 = str1.split('');
    var str8 = str7.push(str2, 'ay');
    var str9 = str7.splice(0,1);
    var str10 = str7.join('');
    return str10;
  } else if (str2 && str2_1 !== 'a' || str2 && str2_1 !== 'e' || str2 && str2_1 !== 'i' || str2 && str2_1 !== 'o' || str2 && str2_1 !== 'u') {
    var str11 = str;
    var str12 = str1.split('');
    var str13 = str12.push(str2, str2_1, 'ay');
    var str14 = str12.join(''); 
    return str14;

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


While I understand you want to solve this in your own way, your approach is very hard to read. You have way too many variables and then on top of that you have generic names for them like str1, str2, str14. At least try to name them in such a way, so that anyone reviewing your code will know what each variable contains without having to look back were it is initialized each time. For example, you don’t need str1 at all, because you never reference it again. You could instead just reference str (which is the original string passed into the function) instead of referencing str1 in your code. Also, to make your code more readable, you could rename str2 to be firstLetter and str2_1 could be secondLetter. At least then it is not so confusing.

The reason your code is not returning the correct answer for the test case of “glove”, is because the following else if statement evaluates to true.

} else if (str2 !== 'a' || str2 !== 'e' || str2 !== 'i' || str2 !== 'o' || str2 !== 'u') {

and then the code you have in the else if block executes which ends up returning “lovegay” instead of the correct answer of “oveglay”. Why does the else if condition evaluate to true? Because, str2 in this test case is the letter “g” and “g” is not a vowel.


You really should try to think about his problem in a more generic way. With the current logic, what would you do with a test case of “school” or “schnapps” or a word with even more consonants at the beginning. You can see with your current approach, you are going to have to create so many more variables and many more if else statements.

Without writing any code, why don’t you write out in normal language (on paper) the steps you would take to actually translate any given word over to pig latin.

Maybe you should first figure out what different types of cases you could run into. One case need to think about is a word multiple consecutive consonants. For this case, you need to think about how you could iterate through the letters in the word to see when you encounter the first vowel and then build the applicable string based on the index of the first vowel location to the beginning of the string.

Another case is a word starts with a vowel. That one is pretty straight forward with what to return.

Are there other special cases? Think about other scenarios which could change how you build the string to return.

1 Like