· the if clause that produces the termination of the recursive calls
· the recursive call
The structure that statement describes was the template we used in all of our solutions.
The problem statement in the Fibonacci exercise gave us another tool that we reused in subsequent exercises, a chart of the values of the function's fields over the steps of the recursive call. Although we had these tools that were useful later, we had to ask for help and Garrett coached us to the solution.
The problem statement tells us that for n > 1, fib(n) is fib(n - 1) + fib(n - 2)
What follows is the chart given to us in the problem then the Fibonicci function, followed by a version of the Fibonicci function that includes some logging statements. The console.log statements provide a view into the functioning of the function.
What allowed us to complete the problems was our willingness to try something we were pretty sure would not work and then diagnose the non-working code to determine the necessary changes.
Flavors of Recursive Functions
After receiving help from an instructor, we found that return statements in recursive functions come in two flavors:1. Return statements that rely only on the function call:
· return fib(n - 1) + fib(n - 2);
· return isEven(n - 2);
· return add(dec(x), inc(y));
2. Return statements that rely on the function call plus values outside of the recursive call to the function:
· return start + sum(start + 1, end);
· return y + multiply(x - 1, y);
· return start * product(start + 1, end);