Recursion With Short Circuit Return Statements

I’m reading the Eloquent JavaScript book and this recursive function is bugging me in one thing. The return statement. Here is the code from the book:

``````function findSolution(target) {
function find(current, history) {
if (current == target) {
return history;
} else if (current > target) {
return null;
} else {
return find(current + 5, `(\${history} + 5)`) ||
find(current * 3, `(\${history} * 3)`);
}
}
return find(1, "1");
}

console.log(findSolution(3));
// → (1 * 3)
``````

The first two return are simple and straight forward but I can’t figure out how, after executing the left side(true value) of the short circuit evaluation, the same call can re execute and try the right side of the evaluation.

Let me clear this up:
-We call `findSolution(3);`
-Run past the actual recursive function to call it for the first time with `find(1, ...)` (I’ll keep the history with ‘…’ for typing sakes).

• 5 get’s added to current(1 is a true value) and we now call `find(6, ...)`
-Since 6 > 3 we get a value returned which is `null`
-So now `find(1, ...)` has to deal with null`.
And this is the part that get’s me.
`find(1, ...)` received it’s answer(null) from `find(6, ...)`

Why does it run again and executes the right side of the short circuit?

I’m assuming the short circuit it self is a function operating independent from the return statement.
So the return from `find(1, ...)` get’s put on hold until we can solve the short circuit? Null is a value to be returned.
I’m a bit lost there…

so now, the left hand-side of the or operator (`||`) is false, in other words:

``````return false || find(current * 3, `(\${history} * 3)`);
``````

so now the right hand side of the operator is executed first. `find(6, ...)` is not happening

Is the the short-circuit also for `or` operator? Anyway, the short circuit only happens the left hand side evaluates to true, given both `true || false` and `true || true` are both true. but your left-hand side is false, so the right side of the `or` operator is evaluated

this tool might help:

Python Tutor - Visualize Python, Java, JavaScript, C, C++, Ruby code execution

1 Like

Ok so, `find(6, ...)` won’t run.
I think I get it…
Each side of the operator has a function call. So `find(1, ...)` return statement is waiting to see who is going to evaluate to true?
And the side that turns out to be true get’s executed?
And that side is yet another function call.

the standard `||` rules apply here, first the left-side is evaluated, and then the right hand-side. the recursion seems to throw you off

I’m not sure it does. Maybe it’s the || operator.
From the book
" The `||` operator denotes logical or . It produces true if either of the values given to it is true."
Also for short circuit specifically
" The `||` operator, for example, will return the value to its left when that can be converted to true and will return the value on its right otherwise."
So if the left side is false it executes the right side without checking if the value is actually true or false.

I do not understand this sentence. If the left-hand side is false, the right-hand side of the `or` operator is executed. The result of this execution is then returned

That’s why for `findSolution(2)` we get null?
Left-hand side is false so the right side is return. Which is null.

yes

well, technically the left-hand side is also `null`, which is a falsy value (aka considered false)

Yeah you answered me 3 times and I would not get it. Sorry about that. Now everything you said makes sense to me. It’s was the || operator combined with a null/false value. I don’t know why it took me so long to figure it out…
Ty so much @stetim94!!!
This one was starting to get to me. Sorry it took me so long.

1 Like

You’re welcome

yea, there is a lot happening in this code. Being able to dissect the code and understanding the different parts might be overwhelming, which is why it takes longer we get there in the end

1 Like