Pig Latin , seems like maybe there's a bug?

Tell us what’s happening:
this is for the pig latin algorithm , there’s basically 3 rules(to my understanding)
1 - if starts with vowel ==> add ‘way’ to the end
2 - if starts with a consonant , cut the word until the forst vowel , add it to the end and add to it ‘ay’
3 - no vowel in the word (like with ‘shh’), just add ‘ay’ to the end

Maybe i’m getting crazy , but i console.log all possible places , and everything seems to work…
i mean my code is quite straight forward ,
- a variable with vowels
- then check first letter of input to see if match ,
yes - add the ‘way’
no - keep going to an IF statement
- IF ( check if there’s no vowels)
yes - no vowels , so the “handle words without vowels” should be checked // but it doesn’t…
no - go to the else , where count how many letters until vowel, then cut , ut at end and add the ‘ay’
so it seemed simple , but is driving me to hell …
is there something i missed ??
Please i need a hand

Thanks to everyone in advance and sorry if there’s something i did completely wrong , but i’m so close to the tree i cant see the forest.

Your code so far


function translatePigLatin(str) {
  const regex = /[aeiouy]/gi;
  let result = '';
    if (str[0].match(regex)) {
    result = str + 'way';
  } else{
        if(/^[^aeiuoy]+$/.test(str)){
              result = str + 'ay';     
        }else{
                const vowelIndex = str.indexOf(str.match(regex)[0]);
              result = str.substr(vowelIndex) + str.substr(0, vowelIndex) + 'ay';
        }
  }
  console.log(result);
  return result;
}

translatePigLatin("sdfgh");


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0.

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

y isn’t a standard vowel in English. It is vowel-like under some conditions, but it isn’t a vowel (otherwise you’d need to include w as well).

i thought that needed to put it as for the word ‘my’ , is a vowel like use… but didn’t think of the w …
i just tried it without it and it passed .
Thank you Dan

1 Like