2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
id: a302f7aae1aa3152a5b413bc
|
|
|
|
title: Factorialize a Number
|
|
|
|
challengeType: 5
|
2019-07-31 11:32:23 -07:00
|
|
|
forumTopicId: 16013
|
2018-09-30 23:01:58 +01:00
|
|
|
---
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --description--
|
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
Return the factorial of the provided integer.
|
2020-11-27 19:02:05 +01:00
|
|
|
|
2018-09-30 23:01:58 +01:00
|
|
|
If the integer is represented with the letter n, a factorial is the product of all positive integers less than or equal to n.
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
Factorials are often represented with the shorthand notation `n!`
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
For example: `5! = 1 * 2 * 3 * 4 * 5 = 120`
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
Only integers greater than or equal to zero will be supplied to the function.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --hints--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`factorialize(5)` should return a number.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(typeof factorialize(5) === 'number');
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`factorialize(5)` should return 120.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
assert(factorialize(5) === 120);
|
|
|
|
```
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`factorialize(10)` should return 3628800.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```js
|
2020-11-27 19:02:05 +01:00
|
|
|
assert(factorialize(10) === 3628800);
|
|
|
|
```
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`factorialize(20)` should return 2432902008176640000.
|
|
|
|
|
|
|
|
```js
|
|
|
|
assert(factorialize(20) === 2432902008176640000);
|
2018-09-30 23:01:58 +01:00
|
|
|
```
|
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
`factorialize(0)` should return 1.
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
assert(factorialize(0) === 1);
|
|
|
|
```
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --seed--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
## --seed-contents--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
```js
|
|
|
|
function factorialize(num) {
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
factorialize(5);
|
|
|
|
```
|
2018-09-30 23:01:58 +01:00
|
|
|
|
2020-11-27 19:02:05 +01:00
|
|
|
# --solutions--
|
2018-09-30 23:01:58 +01:00
|
|
|
|
|
|
|
```js
|
|
|
|
function factorialize(num) {
|
|
|
|
return num < 1 ? 1 : num * factorialize(num - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
factorialize(5);
|
|
|
|
```
|