Pig Latin - words without vowels

Hi campers,

I need your guidance. I have been working on the algorithm challenge “Pig Latin” today and I thought I had resolved this, but when I tested it on FCC, my solution didn’t pass the following test: Should handle words without vowels.

Could you look at my code and help me out? I assume the words without vowels would just have “ay” appended as the first half of the word, but maybe I am wrong.

Thanks in advance for your help :slight_smile:

Your code so far*


function translatePigLatin(str) {

  let firstVowelIndex= str.search(/[aeiou]/);
  let firstConsonant = str.split("")[0];
  let noVowels = /^[^aeyiuo]+$/.test(str);
  let firstLetter = str.split("")[firstVowelIndex];
  let firstHalf = str.split("").slice(0, firstVowelIndex).join("");
  let secondHalf = str.split("").slice(firstVowelIndex).join("");
  let lastLetter = str.split("")[str.length -1];

    if (/[aeiou]/.test(str.split("")[0])) {
    return str + "way";
  }
  else if (noVowels) {
    return str + "ay";
  }
    else {
      return secondHalf + firstHalf + "ay";
    }

}

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/67.0.3396.99 Safari/537.36.

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

For a word like rhythm, your code returns mrhythay instead of rhythmay.

The problem is in the following line of code:

let noVowels = /^[^aeyiuo]+$/.test(str);

“y” is not a vowel in the English language which is what the challenge instructions have indicated the words are.

Thank you so much!! Now it’s time for me to look at other solutions and make my code (much) more elegant :slight_smile: