UNPKG

@onesy/algorithms

Version:
15 lines 536 B
const cache = {}; export default function factorialRecursive(value) { let options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : { cache: true }; // Cache if (options?.cache && cache[value] !== undefined) return cache[value]; if (value < 3) { if (options?.cache && cache[value] === undefined) cache[value] = value; return value; } const result = value * factorialRecursive(value - 1, options); if (options?.cache && cache[value] === undefined) cache[value] = result; return result; }