UNPKG

@mathlib/functions

Version:
21 lines (20 loc) 535 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function factorial(n) { if (isNaN(n)) { throw new Error("n should be a number."); } let _n = typeof n === "string" ? parseFloat(n) : n; if (!Number.isInteger(_n)) { throw new Error("Factorial of fractions are not supported."); } if (_n < 0) { throw new Error("n should not be negative."); } let fact = 1; while (_n > 1) { fact *= _n--; } return fact; } exports.default = factorial;