(n, cache = null)
| 6 | * @returns {number} |
| 7 | */ |
| 8 | export const fibonacciCache = (n, cache = null) => { |
| 9 | if (cache) { |
| 10 | const value = cache.get(n) |
| 11 | |
| 12 | if (value !== null) { |
| 13 | return value |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | if (n === 1 || n === 2) { |
| 18 | return 1 |
| 19 | } |
| 20 | |
| 21 | const result = fibonacciCache(n - 1, cache) + fibonacciCache(n - 2, cache) |
| 22 | |
| 23 | cache && cache.set(n, result) |
| 24 | |
| 25 | return result |
| 26 | } |
| 27 | |
| 28 | /** |
| 29 | * @title implementation of union function |
no test coverage detected