605 B
605 B
title, type, language, tags, cover, dateModified
| title | type | language | tags | cover | dateModified | |||
|---|---|---|---|---|---|---|---|---|
| Factorial of number | snippet | javascript |
|
flower-vase | 2020-12-28T13:49:24+02:00 |
Calculates the factorial of a number.
- Use recursion.
- If
nis less than or equal to1, return1. - Otherwise, return the product of
nand the factorial ofn - 1. - Throw a
TypeErrorifnis a negative number.
const factorial = n =>
n < 0
? (() => {
throw new TypeError('Negative numbers are not allowed!');
})()
: n <= 1
? 1
: n * factorial(n - 1);
factorial(6); // 720