sussy-util
Version:
Util package made by me
24 lines (23 loc) • 934 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* "If the index is not in the cache, then add the sum of the previous two numbers to the cache and
* return it."
* @param {number} index - The index of the number in the Fibonacci sequence to return.
* @param {number[]} cache - This is an array that will store the values of the fibonacci sequence.
* @returns The nth number in the fibonacci sequence.
*/
const fibonacci = (index, cache = [0, 1]) => {
if (index < 0) {
throw new Error('Index must be greater than or equal to 0.');
}
if (cache[index] === void 0) {
cache[index] = fibonacci(index - 1, cache) + fibonacci(index - 2, cache);
}
return cache[index];
};
/**
* @param {number} index - The index of the number in the Fibonacci sequence to return.
* @returns The nth number in the fibonacci sequence.
*/
exports.default = (index) => fibonacci(index);