mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
merge: Add test case (#851)
* Add test case * minor fix * delete files * rename file
This commit is contained in:
18
Recursive/Factorial.js
Normal file
18
Recursive/Factorial.js
Normal file
@ -0,0 +1,18 @@
|
||||
/**
|
||||
* @function Factorial
|
||||
* @description function to find factorial using recursion.
|
||||
* @param {Integer} n - The input integer
|
||||
* @return {Integer} - Factorial of n.
|
||||
* @see [Factorial](https://en.wikipedia.org/wiki/Factorial)
|
||||
* @example 5! = 1*2*3*4*5 = 120
|
||||
* @example 2! = 1*2 = 2
|
||||
*/
|
||||
|
||||
const factorial = (n) => {
|
||||
if (n === 0) {
|
||||
return 1
|
||||
}
|
||||
return n * factorial(n - 1)
|
||||
}
|
||||
|
||||
export { factorial }
|
@ -1,11 +0,0 @@
|
||||
// function to find factorial using recursion
|
||||
// example :
|
||||
// 5! = 1*2*3*4*5 = 120
|
||||
// 2! = 1*2 = 2
|
||||
|
||||
export const factorial = (n) => {
|
||||
if (n === 0) {
|
||||
return 1
|
||||
}
|
||||
return n * factorial(n - 1)
|
||||
}
|
11
Recursive/test/Factorial.test.js
Normal file
11
Recursive/test/Factorial.test.js
Normal file
@ -0,0 +1,11 @@
|
||||
import { factorial } from '../Factorial'
|
||||
|
||||
describe('Factorial', () => {
|
||||
it('should return factorial 1 for value "0"', () => {
|
||||
expect(factorial(0)).toBe(1)
|
||||
})
|
||||
|
||||
it('should return factorial 120 for value "5"', () => {
|
||||
expect(factorial(5)).toBe(120)
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user