mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-07 19:17:33 +08:00
Move the (num > 0) if condition to the end
This commit is contained in:
@ -18,11 +18,6 @@ const calcRange = (num) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const calcFactorial = (num) => {
|
const calcFactorial = (num) => {
|
||||||
if (num > 0) {
|
|
||||||
const range = calcRange(num)
|
|
||||||
const factorial = range.reduce((a, c) => a * c, 1)
|
|
||||||
return `The factorial of ${num} is ${factorial}`
|
|
||||||
}
|
|
||||||
if (num === 0) {
|
if (num === 0) {
|
||||||
return 'The factorial of 0 is 1.'
|
return 'The factorial of 0 is 1.'
|
||||||
}
|
}
|
||||||
@ -32,6 +27,11 @@ const calcFactorial = (num) => {
|
|||||||
if (!num) {
|
if (!num) {
|
||||||
return 'Sorry, factorial does not exist for null or undefined numbers.'
|
return '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}`
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export { calcFactorial }
|
export { calcFactorial }
|
||||||
|
Reference in New Issue
Block a user