Given a number the program should return a sequence of operations using only *3 or +5 to get to the number, thus there are two paths to take. How do this program know which function call to make when it calls itself? And how does it know how many times to call each path. In other words I dont understand how the OR
operator is being used to determine which call to find()
to use and how many of each.
function findSequence(goal) {
// we start at 1, and history is a string that will keep track of the operations
function find(start, history) {
// case when start and goal is 1.
if (start == goal)
return history; // return string containg 1
// case when we build start past what we needed
else if (start > goal)
return null;
else
// Dont understand this part!
return find(start + 5, "(" + history + " + 5)") ||
find(start * 3, "(" + history + " * 3)");
}
return find(1, "1");
}
document.write(findSequence(13));