Can someone explain how does this code work? It is a code on recursion js from the codeacademy. The code determines rate of growth using The Fibonacci sequence. The output of the code
var height = growBeanstalk(5) // 5
var height = growBeanstalk(8) // 21 etc.
(4) => //3 (3) => //2
Thank you in advance!
function growBeanstalk(years) {
// Base case
if (years <= 2) {
return 1;
}
// Recursive case
return growBeanstalk(years - 1)+ growBeanstalk(years - 2);
}
// Set the height of the beanstalk using your function
var height = growBeanstalk();
console.log(height);