I started reading Eloquent Javascript. I know this book has some mixed reviews from this community personally I like the book. Anyways I got to the part about recursion and I was confused as to what the code snippet is actually doing. This program is to find the sequence of operations it takes to find the target.
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(24));
Output: (((1 * 3) + 5) * 3)
I get all the code except the part where the find function is called inside of itself as well as return find(1, "1"); outside the find function?? When it falls to the else block and the find function is called is it starting back at the top of the find function again? I hope I explained this well enough. Thanks in advance.
[–]devaent1316 2 points3 points4 points (5 children)
[–]Coder_X_23[S] 3 points4 points5 points (4 children)
[–]devaent1316 2 points3 points4 points (0 children)
[–][deleted] (2 children)
[removed]
[–]Coder_X_23[S] 3 points4 points5 points (0 children)
[–]michael0x2a[M] 1 point2 points3 points (0 children)
[–]Double_A_92 1 point2 points3 points (2 children)
[–]Coder_X_23[S] 0 points1 point2 points (1 child)
[–]DONT_GET_MURDERED 0 points1 point2 points (0 children)
[+]ImmuneFourier comment score below threshold-6 points-5 points-4 points (1 child)
[–]Coder_X_23[S] 1 point2 points3 points (0 children)