The intermediate solution doesn’t work without vowels for example: translatePigLatin(“bcdf”); // output “too much recursion”
The advanced solution goes in infinite loop with the same input translatePigLatin(“bcdf”);
And I think I have a much simpler solution (not sure since I am still a noob hehe)
function translatePigLatin(str) {
let vowel = /^[aeiou]/i;
let conson = /^[bcdfghjklmnpqrstvwxyz]+/i;
// Check if the string start with a vowel and then return string + way
// Otherwise replace the first consonant by empty char then add the first consonant + ay at the end
vowel.test(str) ? str += 'way' : str = str.replace(conson.exec(str), '') + str.match(conson)[0] + 'ay';
console.log(str);
return str;
}
translatePigLatin("bcdf");