Fibonacci Sequence (Javascript in HTML)
function fib(first, second, countdown){
console.log("I was called with " + first + ", " + second + ", " + countdown);
if(countdown>0){
fib(first,first+second, countdown-1);
}
}
fib(1,2,3);
Now you get to figure out how to use the fib function to print the number sequence without using a loop in the function (hint, the function needs to call itself). How is this exactly done? It needs to be in written so I can input it into my visual style code which is set up for html.
CODE SCREENSHOT
CODE TEXT
// function to print the nth term of a fibonacci series
function fib(num) {
// if the value of n < 2, return the value of n
// this is the base case
if(num < 2) {
return num;
}
// otherwise perform recursion
else {
return fib(num-1) + fib(num - 2);
}
}
console.log(fib(6))
OUTPUT
Get Answers For Free
Most questions answered within 1 hours.