console.log will print the value returned by findSolution (24), where 24 is the target.
Now, when the findSolution () function is executed, it calls (calls) the "find" function with two parameters: start = 1, history = "1", and then returns something for printing. A.
The βreturnβ is sent back to the place where the call was established. the find () value is called inside findSolution (), so return will send back the value at which it was called in findSolution.
in our case:
if start === target
return history //(to findSolution. let call it "H") return H to console.log() // prints 1
else if start> target
return null //(to findSolution, let call it N) return N to console.log() // prints null
otherwise, if the start is less than the target, return A || B
A = find(start + 5, "(" + history + " + 5)") B = find (start * 3, "(" + history + " * 3)")
means return A, if A is true, otherwise returns B; A or B are evaluated before the function returns.
skip step by step:
find will be called again and again with the initial parameter increased by 5 i.e.:
return find(1, "1") // start = 1, history = "(1 + 5)" return find(6, history) // start = 6, history = "(1 + 5 ) + 5" return find(11, history) // start = 11, history = "(1 + 5) + 5 ) + 5" return find(16, history) // start = 16, history = "(1 + 5) + 5 ) + 5 ) + 5" return find(21, history) start = 21, history = "(1 + 5) + 5 ) + 5 ) + 5 ) + 5"
now the next value will be 26. since 26> 22, "find" will return B instead
return find(63, history) start = 21 * 3 = 63, //since 63 > 24, "null" is returned where start = 16,
start is 16, not 63 because it is a parameter. It does not change and does not know anything about incrementing by 5, which occurred during the call.
find (21 * 3, history), 48> 24, so we return null, where start = 11
find (11 * 3, history), 33> 24, so we return null, where start = 6
find (6 * 3, history), start = 18 and 18 <24, so A will be a true start, will be increased by 5
return find (18 + 5, history)
return find (23 + 5, history) 28> 24, A is N, B is evaluated
... and so on and so forth, until the start is equal to 24