Pig Latin 2 last tests

Tell us what’s happening:
Hey guys, I’m trying to pass this 2 test, but I can’t:

  • Should handle words where the first vowel comes in the end of the word.
  • Should handle words without vowels.

Could someone help me understand this topics?

:smile: D

Your code so far


function translatePigLatin(str) {
  if (/^([b-df-hj-np-tv-z]){2,}/.test(str.slice(0, 2))) {
    const con = str.slice(0, 2);
    let _str = str.slice(2);
    return _str + con + "ay";
  }
  if (/^([b-df-hj-np-tv-z]){1,}/.test(str.slice(0, 1))) {
    const con = str.slice(0, 1);
    let _str = str.slice(1);
    return _str + con + "ay";
  }
  return str + "way";
}

translatePigLatin("consonant");

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36.

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

Look at your slice methods, you are checking only first two letters. If it is not there you are returning the word plus way

Consider what happens if the word doesn’t have a vowel or the vowel is at the end of the word… It passes first condition as first two letters are consonants and the first two letters are moved to the end of the word.

Try solving this keeping this in consideration

1 Like

Thanks mate!
I’ve had to refactor the logic, but it’s working now. :smiley: