Here is what is happening
The function findSolution() is triggered with target quals 24
and it should return find(1, "1")
Now the function find() if (current == target) // current is 1 and target is 24 so this is not true else if (current > target) // current which is 1 is not larger that target
1. function findSolution(target) {
2. function find(current, history) { ... }
3. return find(1, "1");
4. }
On line 2 we declare the function find() and only on line 3 we invoke it with the following options: current = 1 and history = "1". And once the find(1, "1") is invoked the recursion begins.
There are 42 steps at all. On step 15 the find() returns the null value. How is that possible that on step 16 it forgets the history which was equal to "(((1 + 5) + 5) + 5)" on the step 15 and is equal to "((1 + 5) + 5)" on step 16.
when you have ||, if the first one is a falsy value (in this case if the first function return null, the other is executed)
here visualization of what each function returns, it is first tried to add 5, and if that doesn’t bring to anything, then to multiply by 3, note that once a null is returned a new branch is temted, so the history changes again, but it is not forgotten, just discarded because that history brings to a null value
in most cases with recursion you have a a straight line, but the principle is the same, keep going till the returned value is not a function, and then each function will return that value till it reaches the outside