SHARE
TWEET

Untitled

a guest Feb 25th, 2020 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function fibonacci(n) {
  2.     var listFibo = [0]; // first element is defined to 0;
  3.     fibonacciRecursive(n, listFibo);
  4.     return listFibo;
  5. }
  6.  
  7. function fibonacciRecursive(num, memo) {
  8.     var validInput = typeof (num) != "number"
  9.     || Number.isInteger(num) == false
  10.     || num <= 0
  11.    
  12.     if (validInput) throw new Error("Wrong input");
  13.     else {
  14.         memo = memo || {};
  15.         let index = num - 1;
  16.         if (memo[index]) return memo[index];
  17.         if (num === 1) return memo[index];
  18.         if (num === 2) return memo[index] = 1;
  19.         return memo[index] = fibonacciRecursive(num - 1, memo) + fibonacciRecursive(num - 2, memo);
  20.     }
  21. }
  22. console.log(fibonacci(10));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top