Intermediate Algorithm Scripting - Pig Latin

Tell us what’s happening:
I am having trouble determining why when a user submits a word that does not contain any vowels into my function, the same string is returned rather that the string + “ay”.

Your code so far

function translatePigLatin(str) {
  let regex = /^[aeiou]/;
  let noVowelRegex = /[aeiou]/g;
  if (!regex.test(str)){
    str = str.replace(/([^aeiou]+)([aeiou]\w+)/, "$2$1ay");
  }else if (regex.test(str)){
    str = str + "way";
  }else if (str.match(noVowelRegex) == null){
    str = str + "ay";
  } return str;
    
}

console.log(translatePigLatin("lynx"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Pig Latin

Link to the challenge:

I actually figured it out. I’m sure I could have coded the entire thing way more efficiently, but the main issue here is with the ordering of my If/Else chain. The first condition is being met for words that do not contain vowels yet no alterations to the string are made due to the fact that there are no vowels present. By putting the first condition in the If/Else chain as the last condition to be checked, this allows things to run properly

1 Like

This same issue got me on the Record Collection problem. Great work!