mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
fix: Enhance error handling in factorial function (#1430)
This commit is contained in:
@ -9,17 +9,14 @@
|
||||
*/
|
||||
|
||||
const factorial = (n) => {
|
||||
if (!Number.isInteger(n)) {
|
||||
throw new RangeError('Not a Whole Number')
|
||||
}
|
||||
|
||||
if (n < 0) {
|
||||
throw new RangeError('Not a Positive Number')
|
||||
if (!Number.isInteger(n) || n < 0) {
|
||||
throw new RangeError('Input should be a non-negative whole number')
|
||||
}
|
||||
|
||||
if (n === 0) {
|
||||
return 1
|
||||
}
|
||||
|
||||
return n * factorial(n - 1)
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user