Question

JavaScript Write a function using recursion to compute the Fibonacci number of n (where n is...

JavaScript

Write a function using recursion to compute the Fibonacci number of n (where n is a positive integer). Your function should output the calculated result for the n given. You also need to type check to make sure the value being given is an integer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
function myFib(n) {
    if (isNaN(n) || Math.floor(n) !== n)
        return "Not an integer value!";
    if (n === 0 || n === 1)
        return 3;
    else
        return myFib(n - 1) + myFib(n - 2);
}

console.log(myFib(5));
console.log(myFib(10));
console.log(myFib(8));
console.log(myFib("Thirtyfive"));
console.log(myFib(3.5));
Add a comment
Know the answer?
Add Answer to:
JavaScript Write a function using recursion to compute the Fibonacci number of n (where n is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Haskell: Write a recursive function fibonacci that computes the n-th Fibonacci number. fibonacci :: Int...

    In Haskell: Write a recursive function fibonacci that computes the n-th Fibonacci number. fibonacci :: Int -> Int Write a recursive function myProduct that multiplies all the numbers in a list. myProduct :: [Integer] -> Integer Using the technique of List Comprehension write a function that would remove even numbers from a list of lists. For Example: given input: [[1,2,3,4], [6,3,45,8], [4,9,23,8]] expected output: [[1,3], [3,45],[9,23]]. Show how the library function replicate :: Int -> a-> [a] that produces a...

  • Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence....

    Write a program in MIPs Assembly Language to compute nth number of a fibonacci number sequence. Your program should prompt for an integer input n from the user. The program should call a recursive function to compute the nth fibonacci number. Your program must follow programming convention. You should submit program and screenshot of output in a single word/pdf file. You should use following recursive definition of fibonacci function: fib(0) = 0 fib(1) = 1 fib(n) = fib(n-1) +fib(n-2)

  • Consider Fibonacci number F(N), where N is a positive integer, defined as follows. F(1) = 1...

    Consider Fibonacci number F(N), where N is a positive integer, defined as follows. F(1) = 1 F(2) = 1 F(N) = F(N-1) + F(N-2) for N > 2 a) Write a recursive function that computes Fibonacci number for a given integer N≥ 1. b) Prove the following theorem using induction: F(N) < ΦN for integer N≥ 1, where Φ = (1+√5)/2.

  • ARM assembly language Write a program "fibonacci.s" that computes the Nth Fibonacci number where N is...

    ARM assembly language Write a program "fibonacci.s" that computes the Nth Fibonacci number where N is not so large that overflow of integer arithmetic is a concern. When your assembly language program is called it should expect the value of N to be passed using register r0 and your program should return the Nth Fibonacci number in register r0. Please include comments as well. Do not just use the output generated by gcc -S

  • use javascript please Complete the function below using recursion. Assume the parameter is a positive integer....

    use javascript please Complete the function below using recursion. Assume the parameter is a positive integer. The function will return an array that contains all of the numbers from 1 up to the parameter in consecutive order. For example, if the parameter is the value 5 then the return value should be the array [1, 2, 3,4 function makeSequence(value) t\ please use recursion thank von

  • MATLAB 1. The Fibonacci sequence is defined by the recurrence relation Fn = Fn-1+Fn-2 where Fo...

    MATLAB 1. The Fibonacci sequence is defined by the recurrence relation Fn = Fn-1+Fn-2 where Fo = 0 and F1 = 1. Hence F2 = 1, F3 = 2, F4 = 3, etc. In this problem you will use three different methods to compute the n-th element of the sequence. Then, you will compare the time complexity of these methods. (a) Write a recursive function called fibRec with the following declaration line begin code function nElem = fibrec (n) end...

  • Write a program that computes the Fibonacci number for some input integer. (See segments 7.37 –...

    Write a program that computes the Fibonacci number for some input integer. (See segments 7.37 – 7.41 in your book for some info on the Fibonacci sequence or you can look it up on the Internet). The sequence is defined as the first two elements are each 1, after that each element in the sequence is the sum of the previous two elements. The first few numbers of the sequence are 1,1,2,3,5,8,13,21,34,55,… Your assignment is to write a program with...

  • ** C++ LANGUAGE ** Write a function that will implement each Fibonacci number with the help...

    ** C++ LANGUAGE ** Write a function that will implement each Fibonacci number with the help of an integer array of size 100 (elements of this array will be digits of the Fibonacci number). When the function is called to find , it will calculate all Fibonacci numbers from to using the basic formula . To add two Fibonacci numbers, the function will add elements of two arrays corresponding to and and store their sums in the array corresponding to...

  • 1. Recursion is ususally where a function calls itself; (another example of recursion is where function...

    1. Recursion is ususally where a function calls itself; (another example of recursion is where function A calls function B, and B calls C and C calls A, creating a loop in the calls). Some problems are most naturally solved recursively, such as writing the factorial function, or finding all the perumutations of a sequence, or checking if a string is a palindrome. Since those examples were done in class, here we will give you a toy example, which normally...

  • Write the code in java programming language To get some practice with recursion. You can do...

    Write the code in java programming language To get some practice with recursion. You can do all this in one driver program. Write a recursive method to compute the result of the Fibonacci sequence: Fibonacci(N) = Fibonacci(N -1) + Fibonacci(N-2) N == 0 is 0 N == 1 is 1 Testing: Display the result for Fibonacci(N) and the number of function calls for N = 2, 5 and 10.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT