Pig Latin first vowel at the end

I can’t seem to pass one test in the “Pig Latin” problem, which is “Should handle words where the first vowel comes in the end of the word.”

My code is an ugly mess so before looking at it, I just want to know what result I should get to pass this test. There’s nothing in the instruction that says how a word having its first vowel at the end should be returned. What should a string Like “the” return ? “theay” ? “ethay” or something else ?

Your code so far


function translatePigLatin(str) {
  var result = str;
  if (/^[aeiou]/.test(result[0])) { 
  //word begins with a vowel
    result+= 'way'
    return result;
    }
    else if (!(/[aeiou]/.test(result))){ 
    // no vowels in word
      return result+= 'ay';
    }
    
    else if (/[aeiou]/.test(result[result.length - 1]) && !(/[aeiou]/).test(result.substring(0, result.length - 1))){ 
    // first vowel at end of word
      result = str.replace(/(^[^aeiou]{0,3})(\w+)/, '$2$1ay');
      return result;   
    }
    else if(/^[^aeiou]/.test(result[0])){ 
    // word begins with consonant cluster
      result = str.replace(/(^[^aeiou]{0,3})(\w+)/, '$2$1ay');
      return result ;
    } 
}

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

I do believe “the” should become “ethay”. You’re taking the consonants from the start and moving them to the end, before appending “ay”.

Hmm I do get “ethay” from “the”, now I wonder why it doesn’t pass the test.

I don’t know if it’s the reason why it’s not being accepted, but I notice your code doesn’t handle words without vowels correctly. It simply appends “ay” to the end, without moving first consonant to the end.

Well since the instructions don’t say what to do with words without vowels, I simply appended “ay” at the end (first “else if”) and it passes this step.

The only test that doesn’t pass is “Should handle words where the first vowel comes in the end of the word.” all the rest is green.

I am having this same issue… the code passes in the console. but not in the test… very frustrating.

When you log a word with the vowel at the end, such as console.log(translatePigLatin("bllla"));… Does bllla return ablllay? Your code must pass a test like this as well. Perhaps try dividing the word into a root and a suffix and then adding them together to create the pig latin word.

1 Like

Maybe the issue is that there is a separate case for handling words where the only vowel is at the end? When I solved the challenge, the case I wrote for shifting consonants before the first vowel and adding “ay” handled words where the first vowel was at the end. I’ve run into an issue on another challenge where there was a efficiency requirement not explicitly stated that kept the code from passing even though it worked fine in the console.

So, just to prove the point I took your string and separated the code out …

let str="bllla"
let  regex=/[aeiou]/gi;

 var vowelIndice = str.indexOf(regex);
let f= (str.substr(vowelIndice));
let r =str.substr(0,str.length-1);

str=f+r+"ay";
console.log(str);

The Console returns out “ablllay”

So yes

I’m having the same issue. I take care of all of the other checkboxes, and then I give the else statement of

    let arr = str.split("");
    let vowAtEnd = arr.splice(0, length - 1);
    let str3 = vowAtEnd.join("");
    newStr = arr.join("").concat(str3).concat(strAy);
  }

It works if I console.log “the” or “bllla”, but does not pass the test. My code is not elegant, but it works and does not pass this test on FCC. Has anyone solved this?

We really need to see all the code, it would be better if you used the “Ask for help” button on the challenge. It will make a new thread for you and post all the code.

Yeah, I’m currently on this challenge too.
I was able to write the code to solve the first 5 requirements, but the last two:
===> Should handle words where the first vowel comes in the end of the word, and
===> Should handle words without vowels,
I can’t at the moment.

The thing that makes me confused here is that the challenge does not tell us “HOW THESE VOWELS SHOULD BE HANDLED.”

Should words without vowels still follow the same pig latin rule? etc.

After 10 minutes I’m going to check the hint, cos this challenge is giving me a migraine.

Yes, a word without vowels should still be able to have the consonant cluster + ay , as there are no vowels nothing needs to be moved at the end of the word.
For words with vowel at the end: ftrlmno => oftrlmnay

Post your code, use the “Ask for help” button if you want

I’m trying not to give the entire solution, as I understand that’s frowned upon in the forum. I also didn’t want to make a new thread for the same issue described in this thread. Here is my code, though.

 let regex = /[aeiou]/gi, strWay = "way", strAy = "ay", newStr = "", length = str.length;

  if (regex.test(str[0]) === true) {
    newStr = str.concat(strWay);
  }
  else if (regex.test(str[0]) === false && regex.test(str[1]) === true){
    let arr = str.split("");
    let letter = arr.splice(0,1);
    newStr = arr.join("").concat(letter).concat(strAy);
  }
  else if (regex.test(str[0]) === false && regex.test(str[1]) === false && regex.test(str[2]) === true){
    let arr = str.split("");
    let letter2 = arr.splice(0,2);
    let str2 = letter2.join("");
    newStr = arr.join("").concat(str2).concat(strAy);
  } else if (regex.test(str) === false){
    newStr = str.concat(strAy);
  }
  else {
    let arr = str.split("");
    let vowAtEnd = arr.splice(0, length - 1);
    let str3 = vowAtEnd.join("");
    newStr = arr.join("").concat(str3).concat(strAy);
  }

  return newStr;
}


translatePigLatin("consonant");

You can always blur out your code with [spoiler][/spoiler]

Your code seems to work… what have you not passed?

Thanks for the spoiler tip. I haven’t passed the “handles vowels at the end of a word.” Mine will convert the to ethay, and bllla to ablllay, but it doesn’t get the checkmark.

The string you are failing on is “schwartz” and your supposed to return “artzschway”. Consider theoretically that a string (not just the test strings) can be of any length and the vowel can be at any position.

Note: I don’t really understand the assertion text, ‘Should handle words where the first vowel comes in the end of the word.’ how is the vowel in the end? It is supposed to be “near the end” of the word?

Yes! Thank you lasjorg!!

There are words in which the first vowel is the last letter of it

It is not testing for a word ending in a vowel. It is testing for a word where the vowel comes late in the word. Or more precisely, where the vowel is simply further in the string than in the other words tested for.

#13027

  • “schwartz” would be a good test case for handling vowels with index > 2.

The problem is with the phrase “in the end of the word”. It can mean both “near the end” and “at the end”. When I first read it I understood it to mean ending in a vowel, but that is not the case. No word ending in a vowel is tested for. If the test is to catch edge cases, then it should also not word it so the camper might get the idea to just check for the vowel only at the end of the string.

assert.deepEqual(translatePigLatin("schwartz"), "artzschway", 'Should handle words where the first vowel comes in the end of the word.');

I just think the assertion text might be a bit confusing that’s all. Basically, the algorithm should handle all words, of any length, where the vowel is in any position. However, testing for that using just fixed strings presents a problem and is not super reliable. Technically, you can hard-code for all the tests.

1 Like