You can make it simpler to use the "early-return" technique.
function translatePigLatin(str) { const regex = /^[^aeiou]+/g; const consonants = str.match(regex); if (consonants == null) { return str.concat('way'); } else { return str .replace(regex, '') .concat(consonants) .concat('ay'); }
And, another way using ordinally array-like methods (or, not using the regex):
const innerTPL = head => tail => tail === "" ? head === "" ? "" : head + "ay" : "aeiou".includes(tail[0]) ? head === "" ? tail + "way" : tail + head + "ay" : innerTPL(head + tail[0])(tail.slice(1)) const translatePigLatin = innerTPL("")
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You can make it simpler to use the "early-return" technique.
And, another way using ordinally array-like methods (or, not using the regex):