mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-11-13 06:23:00 +08:00
throw-error-for-negative-factorial
This commit is contained in:
parent
bbbfd32a45
commit
85d17f8f64
@ -3,9 +3,14 @@ import factorial from '../factorial';
|
|||||||
describe('factorial', () => {
|
describe('factorial', () => {
|
||||||
it('should calculate factorial', () => {
|
it('should calculate factorial', () => {
|
||||||
expect(factorial(0)).toBe(1);
|
expect(factorial(0)).toBe(1);
|
||||||
|
expect(factorial(-0)).toBe(1);
|
||||||
expect(factorial(1)).toBe(1);
|
expect(factorial(1)).toBe(1);
|
||||||
expect(factorial(5)).toBe(120);
|
expect(factorial(5)).toBe(120);
|
||||||
expect(factorial(8)).toBe(40320);
|
expect(factorial(8)).toBe(40320);
|
||||||
expect(factorial(10)).toBe(3628800);
|
expect(factorial(10)).toBe(3628800);
|
||||||
});
|
});
|
||||||
|
it('should throw exception', () => {
|
||||||
|
expect(() => factorial(-1)).toThrowError('Factorial of a negative number (-1) does not exist!');
|
||||||
|
expect(() => factorial(-10)).toThrowError('Factorial of a negative number (-10) does not exist!');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -3,9 +3,14 @@ import factorialRecursive from '../factorialRecursive';
|
|||||||
describe('factorialRecursive', () => {
|
describe('factorialRecursive', () => {
|
||||||
it('should calculate factorial', () => {
|
it('should calculate factorial', () => {
|
||||||
expect(factorialRecursive(0)).toBe(1);
|
expect(factorialRecursive(0)).toBe(1);
|
||||||
|
expect(factorialRecursive(-0)).toBe(1);
|
||||||
expect(factorialRecursive(1)).toBe(1);
|
expect(factorialRecursive(1)).toBe(1);
|
||||||
expect(factorialRecursive(5)).toBe(120);
|
expect(factorialRecursive(5)).toBe(120);
|
||||||
expect(factorialRecursive(8)).toBe(40320);
|
expect(factorialRecursive(8)).toBe(40320);
|
||||||
expect(factorialRecursive(10)).toBe(3628800);
|
expect(factorialRecursive(10)).toBe(3628800);
|
||||||
});
|
});
|
||||||
|
it('should throw exception', () => {
|
||||||
|
expect(() => factorialRecursive(-1)).toThrowError('Factorial of a negative number (-1) does not exist!');
|
||||||
|
expect(() => factorialRecursive(-10)).toThrowError('Factorial of a negative number (-10) does not exist!');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
* @return {number}
|
* @return {number}
|
||||||
*/
|
*/
|
||||||
export default function factorial(number) {
|
export default function factorial(number) {
|
||||||
|
if (number < 0) throw new Error(`Factorial of a negative number (${number}) does not exist!`);
|
||||||
let result = 1;
|
let result = 1;
|
||||||
|
|
||||||
for (let i = 2; i <= number; i += 1) {
|
for (let i = 2; i <= number; i += 1) {
|
||||||
|
@ -3,5 +3,6 @@
|
|||||||
* @return {number}
|
* @return {number}
|
||||||
*/
|
*/
|
||||||
export default function factorialRecursive(number) {
|
export default function factorialRecursive(number) {
|
||||||
|
if (number < 0) throw new Error(`Factorial of a negative number (${number}) does not exist!`);
|
||||||
return number > 1 ? number * factorialRecursive(number - 1) : 1;
|
return number > 1 ? number * factorialRecursive(number - 1) : 1;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user