Recursive solution to Intermediate Algorithm Scripting problem "Pig Latin""

Tell us what’s happening:

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;


 return str+"way"

 return translatePigLatin(str.substring(1,str.length)+str[0])




Your browser information:

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

Challenge: Pig Latin

Link to the challenge:

There’s no real need for recursion here, just like there is no need for loops. You don’t need to iterate over anything.

I know the solution for it ,I was just trying to solve it recursively as I am pretty bad at it

It’s going to be hard because you are trying to force recursion into a situation that it doesn’t belong in. You can get recursion in there, but why?

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.

Got it.I guess i will stick with other easy approaches :wink:

@jeffy Just because you were interested, I added an additional solution to the hints/solutions topic that shows one possible recursive solution.

I put it together quickly, so I am sure it could be tweaked to make it more concise.

1 Like