forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactorial.js
More file actions
Latest commit
37 lines (30 loc) · 834 Bytes
/
Copy pathFactorial.js
File metadata and controls
37 lines (30 loc) · 834 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*
author: PatOnTheBack
license: GPL-3.0 or later
Modified from:
https://github.com/TheAlgorithms/Python/blob/master/maths/factorial_python.py
This script will find the factorial of a number provided by the user.
More about factorials:
https://en.wikipedia.org/wiki/factorial
*/
'use strict'
constcalcRange=(num)=>{
return[...Array(num).keys()].map(i=>i+1)
}
constcalcFactorial=(num)=>{
if(num===0){
return1
}
if(num<0){
throwError('Sorry, factorial does not exist for negative numbers.')
}
if(!num){
throwError('Sorry, factorial does not exist for null or undefined numbers.')
}
if(num>0){
constrange=calcRange(num)
constfactorial=range.reduce((a,c)=>a*c,1)
returnfactorial
}
}
export{calcFactorial}