Reputation: 924
How does the start
and history
gets a value?. Where it came from?. I am reading this LINK.
If anyone knows please explain it.
Output
(((1 * 3) + 5) * 3)
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));
Upvotes: 0
Views: 35
Reputation: 15642
function findSolution(target) {
function find(start, history) { // <--- NOTICE DECLARATIONS HERE
/* SNIP */
return find(start + 5, "(" + history + " + 5)") ||
find(start * 3, "(" + history + " * 3)");
} // ^--- FUNCTION CALLS HERE
return find(1, "1"); // <--- AND HERE
}
console.log(findSolution(24));
I've snipped out some irrelevant details to this question and inserted some comments. As you can see, the function find
is declared with the two arguments in question start
and history
. find
is first called with 1
as the value for start
and "1"
as the value for history
. Following this, the find
function recursively calls itself with new values for these arguments.
Upvotes: 2