Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write the fibonacci function in JavaScript and please leave comments . The fibonacci function should have one parameter named n and return the nth Fibonacci

Write the fibonacci function in JavaScript and please leave comments . The fibonacci function should have one parameter named n and return the nth Fibonacci number:

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Design Operation And Evaluation Of Mobile Communications

Authors: Gavriel Salvendy ,June Wei

1st Edition

3030770249, 978-3030770242

More Books

Students also viewed these Programming questions

Question

What does javadoc command do?

Answered: 1 week ago