## 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!

### 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. # Help for recursive function exercise!

Hi everyone,

I'm trying to understand this exercise but I do not realize what happens with the OR statement! Supposed to have funcA and funcB in OR statement, what happens at the first cycle? Does it evaluate funcA and funcB or only funcA? Does funcB is evaluated only when funcA is null? Can someone make an example of how does it work step by step? Thanks a lot to everybody!

Ops sorry! This is the code

'''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)"); }

function findSolution(target) { return find(1, "1"); }

console.log(findSolution(34)); '''

No sorry, this is the exercise! I cannot understand the syntax of this code! Can you help me explaining me every step?

Thanks a lot Thomas!!!

Sorry I lost one piece of code :-)

'''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)); ''' I tried to comment an example. But the easiest way to understand recursion is to write it down, like I've done below, and use a tiny example. 34 is way to big for understanding (see below for what I mean...)

```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(3));

/*
Alrigt - so what is happening?

findSolution(3) kicks off the recursion by returning a find()-function with starting conditions:
(Look at the numbers for when the different steps happens (from 1 to 8):)

1) find(1, "1") =>
2) return find(6, "(1 + 5)") || find(3, "(1 * 3)")
8) return "(1 * 3)"

3) find(6, "(1 + 5)") || find(3, "(1 * 3)")
4) return null || find(3, "(1 * 3)")
7) return return "(1 * 3)"

5) null || find(3, "(1 * 3)")
6) return "(1 * 3)"
*/
```

Hi Thomas,

what about for example for the number 13? When you arrive to OR statement is evaluated both funcA and funcB or only funcA till it is null? Can you make example even for the number 13 please? I have problems to understand! Thanks Here is an example for when 13 is passed as the target:

Nr 1 - 20 is for pointing out when every step occurs.

```1) find(1, "1")
2) return find(6, "(1 + 5)")
20) return  "(((1 * 3) + 5) + 5)"

3) find(6, "(1 + 5)")
4) return find(11, "((1 + 5) + 5)")
19) return  "(((1 * 3) + 5) + 5)"

5) find(11, "((1 + 5) + 5)")
6) return find(16, "(((1 + 5) + 5) + 5)"
18) return  "(((1 * 3) + 5) + 5)"

7) find(16, "(((1 + 5) + 5) + 5)"
8) return find(3, "(1 * 3)")
17) return  "(((1 * 3) + 5) + 5)"

9) find(3, "(1 * 3)")
10) return find(8, "((1 * 3) + 5)")
16) return return  "(((1 * 3) + 5) + 5)"

11) find(8, "((1 * 3) + 5)")
12) return find(13, "(((1 * 3) + 5) + 5)")
15) return  "(((1 * 3) + 5) + 5)"

13) find(13, "(((1 * 3) + 5) + 5)")                    <== FINALLY, 13 (source matches the target, meaning we return the history
14) return  "(((1 * 3) + 5) + 5)"
```

Thanks Thomas,

but it's still not clear which steps does the program do. I mean... at the first cycle funcA and funcB are evaluated? When funcA is over the target number it becomes NULL, isn't it? And so what happens? It starts counting funcB? Whith which number? Does it start counting with the previous number? Could you please comment every steps?

Sorry but it looks very difficult for me ti understand!

Hi, but what happens when in a cycle the statement evaluates that both funcA and funcB ges a numbre higher than the target number??? If you refer to the previous example with numbers 1 to 20:

In step 7 when 16 is passed as the source that means funcA is null, which means we go the funcB instead, and start all over again (by passing 1 as the source) which you can see me do in step 8

and If both functions return null, the entire answer will be null

So if during a cycle both functions gets null, the result is null? You mean that it's not possible for example to reach the number 14?

So this means that for the number 14 the result will be null??

When funcA and/or funcB return null, does it start counting backwards to find the number?

I know that the result is null but i asked you this because I still do not understand how the result could be a combination of funcA and funcB. I mean, for the number 14 for example, how does it get the result (((1 * 3) * 3) + 5) ??? If the result of funcA is null and it starts counting with funcB, the result of funcB shouldn't always be a combination of the sign "" and not sign"+"? Sorry, I have difficult to understand this step! FuncB is ''' find(start * 3 , "(" + HISTORY + " * 3 "); ''' so there is a combination of sign "" only!

Could you explain me better this step?

Thanks again!