Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Luca Di Pinto
8,051 PointsHow does this code work???
I came across this example of a recursive function in a textbook. Can you explain me in detail how this function works??
'''function findSolution(target){ function find(start, history){ if(start == target) return history else if(start > target) return null else return find(start + 5, "(" + history + " + 5)") || find(start * 3, "(" + history + " * 3)") } return find(1, "1"); }
console.log(findSolution(34)); '''
Can someone tell me in detail step by step how it works?? I'm losing my head!!!!!! Thanks a lot!!!
5 Answers
james south
Front End Web Development Techdegree Graduate 33,271 Pointswell, it starts with 1 and tries to find a way to achieve the target (34) with various combinations of adding 5 and multiplying by 3. it adds 5 repeatedly first but if it goes beyond the target, it backtracks and tries x3. if that is still too high it backtracks further by removing another +5, and tries x3 again. i can't get the js stepper to work on the linked site but i translated this code to python so you can step through it and see what it is doing:
if you lower the target or change the arguments in the internal call to find to larger numbers like (3,'3') or (29,'29') it will shorten the program somewhat. not every target can be achieved with +5 and x3.
Luca Di Pinto
8,051 PointsWell but it's not clear to me what does OR statement does! In an OR statement with funcA || funcB, they are evaluated in every cycle or funcB is evaluated only when funcA is null?? So, when does funB starts ounting??
james south
Front End Web Development Techdegree Graduate 33,271 Pointsif you step through it with the link i provided you can see the formation and retracement of the "stack" that recursive programs create. as you step through you see it deepen and rise back up several times. it starts with +5 but if that leads to a null or None, it tries x3 for that level in the stack. if that is also null, it moves up a level in the stack and tries again. if you skip to the end you see the answer is 1*3+5*3+5+5=34. only by exhausting all possibilities starting with +5 does it determine the first thing to do is x3. then it starts building a new stack with +5 etc. when you step through you can see it try +5, reach null, then because of the OR, try x3.
Luca Di Pinto
8,051 PointsHi James,
please could you write me step by step what happens in the function if you want to reach the number 14? Thanks a lot!
Luca Di Pinto
8,051 PointsUP!!!