mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2026-03-13 15:21:15 +08:00
fix: factorial function (#1093)
This commit is contained in:
@@ -19,18 +19,18 @@ const calcRange = (num) => {
|
||||
|
||||
const calcFactorial = (num) => {
|
||||
if (num === 0) {
|
||||
return 'The factorial of 0 is 1.'
|
||||
return 1
|
||||
}
|
||||
if (num < 0) {
|
||||
return 'Sorry, factorial does not exist for negative numbers.'
|
||||
throw Error('Sorry, factorial does not exist for negative numbers.')
|
||||
}
|
||||
if (!num) {
|
||||
return 'Sorry, factorial does not exist for null or undefined numbers.'
|
||||
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 `The factorial of ${num} is ${factorial}`
|
||||
return factorial
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user