I am trying to find a recursive solution to the problem “Pig Latin” .So far when i am trying to reach the base case it returns “way” as the suffix for words starting from constant and vowel.Should i change my base case?
Your code so far
function translatePigLatin(str) {
var changed=true;
if(["a","e","i","o","u"].includes(str[0]))
{
return str+"way"
}
else
{
return translatePigLatin(str.substring(1,str.length)+str[0])
}
}
console.log(translatePigLatin("glove"));
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:74.0) Gecko/20100101 Firefox/74.0.
I’m with @ArielLeslie here, recursion isn’t really called for. Typically you use recursion when dealing with data structures that are themself recursively defined, like trees. Arrays count too if you consider them to consist of a head and tail, but those have HOFs like .reduce() and .map() so you don’t usually need hand-rolled recursion for those.
Given the fact that the regex-based solution (hint) uses alternation, a fully recursive solution (yours is partial) would likely need backtracking itself. Certainly doable, but a pretty advanced topic in recursion.