FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript/Maths/Factorial.js at master · TheAlgorithms/JavaScript · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
TheAlgorithms
/
JavaScript
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
5.8k
Star
34.2k
Code
Issues
21
Pull requests
192
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript
/
Maths
/
Factorial.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
39 lines (32 loc) · 848 Bytes
Breadcrumbs
JavaScript
/
Maths
/
Factorial.js
Copy path
File metadata and controls
39 lines (32 loc) · 848 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
38
39
/*
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'
const
calcRange
=
(
num
)
=>
{
return
[
...
Array
(
num
)
.
keys
(
)
]
.
map
(
(
i
)
=>
i
+
1
)
}
const
calcFactorial
=
(
num
)
=>
{
if
(
num
===
0
)
{
return
1
}
if
(
num
<
0
)
{
throw
Error
(
'Sorry, factorial does not exist for negative numbers.'
)
}
if
(
!
num
)
{
throw
Error
(
'Sorry, factorial does not exist for null or undefined numbers.'
)
}
if
(
num
>
0
)
{
const
range
=
calcRange
(
num
)
const
factorial
=
range
.
reduce
(
(
a
,
c
)
=>
a
*
c
,
1
)
return
factorial
}
}
export
{
calcFactorial
}
Back
|
FazBrowse Home
|
New Git URL