Can you explain this recursive solution from Chap 3 Eloquent js?

I am reviewing Chapter 3 in eloquent javascript, and recursion has just been introduced, and I am trying to ponder this concept.

Also can someone explain the second parameter history in the find function?

Is the story defined?

Consider this puzzle: starting from number 1 and repeating either adding 5 or multiplying by 3, you can create an infinite number of new numbers. How could you write a function that, given a number, is trying to find a sequence of additions and multiplications that produce that number? For example, the number 13 can be achieved by first multiplying by 3, and then adding 5 twice, while the number 15 cannot be achieved 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)); 
+5
source share
3 answers

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

+3
source

History is indicated when calling the find function. You cannot call the find function without also specifying a value for the story. Remember that the function will not work until it is called, so the history will not receive a value until it reports that its value is "1" in the line return find(1, "1"); . The history of variables is defined as the thing after the decimal point whenever find .

0
source

Ah, I remember how long I came across him ...

The author defines the find function inside the findSolution function, which is quite confusing

If I rewrote it this way, you get it:

 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(24)); 
0
source

Source: https://habr.com/ru/post/1212063/


All Articles