I am going through chapter 3 in eloquent javascript and recursion was just introduced and I am trying wrap my mind around this concept.
Also can someone explain the second parameter history in the function find ?
Has history already been defined ?
Consider this puzzle: by starting from the number 1 and repeatedly either adding 5 or multiplying by 3, an infinite amount of new numbers can be produced. How would you write a function that, given a number, tries to find a sequence of such additions and multiplications that produce that number? For example, the number 13 could be reached by first multiplying by 3 and then adding 5 twice, whereas the number 15 cannot be reached at all.
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(24));
console.log will print the value returned by findSolution(24) where 24 is the target.
now, when the function findSolution() is executed, it invokes (calls) the function "find" with 2 parameters : start = 1, history = "1" then returns something to be printed.
"return" sends back to the place the call was set. meaning find() is called within findSolution(), hence return will send back a value where it was called within findSolution.
in our case :
if start === target
return history //(to findSolution. let's call it "H")
return H to console.log() // prints 1
else if start > target
return null //(to findSolution, let's call it N)
return N to console.log() // prints null
otherwise if start is lesser than target, return A || B
A = find(start + 5, "(" + history + " + 5)")
B = find (start * 3, "(" + history + " * 3)")
meaning return A if A is truthy otherwise return B; A or B are evaluated before the function returns.
let's go through it step by step :
find will be invoked again and again with start parameter incremented by 5 ie :
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 and not 63 because it is a parameter. It is not changed and knows nothing about the incrementation by 5 that occurred during the invocation.
find(21*3, history) , 48 > 24 so return null where start = 11
find(11 *3, history) , 33 > 24 so return null where start = 6
find(6*3, history) , start = 18 and 18 < 24 so A will be truthy start will be incremented by 5
return find( 18 + 5, history)
return find (23 + 5, history) 28 > 24, A is null, B is evaluated
... and so on and so on until start is equal 24
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With