Question
Write the fibonacci function in JavaScript and please leave comments . The fibonacci function should have one parameter named n and return the nth Fibonacci
If n is a nonnegative integer (positive or zero):
If n is less than 2, the function should return the value of n. This is the base case and will ensure that the recursion eventually stops.
Otherwise, the function should return the sum of fibonacci(n - 2) and fibonacci(n - 1). This is the recursive step.
If n is not an integer or is a negative number, the function should return the default value of 0.
Here are some values to use when testing your code:
The 10th Fibonacci number is 55.
The 20th Fibonacci number is 6765.
The 30th Fibonacci number is 832040.
The On-Line Encyclopedia of Integer Sequences has even more values to test with. When testing, don\'t bother trying input numbers greater than about 40. This recursive algorithm has an exponential running time and is very slow for larger inputs.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started