mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 00:01:37 +08:00
Added tests & normal function convertion (#445)
* added factorial calculator recursively * added tests and converted to normal function * Added tests & normal function convertion * Update factorialCalculator.js updated code "design" not sure why test fails on such arbitrary things. * Update and rename factorialCalculator.js to factorial.js Co-authored-by: vinayak <itssvinayak@gmail.com>
This commit is contained in:
16
Recursive/factorial.js
Normal file
16
Recursive/factorial.js
Normal file
@ -0,0 +1,16 @@
|
||||
// function to find factorial using recursion
|
||||
// example :
|
||||
// 5! = 1*2*3*4*5 = 120
|
||||
// 2! = 1*2 = 2
|
||||
|
||||
const factorial = (n) => {
|
||||
if (n === 0) {
|
||||
return 1
|
||||
}
|
||||
return n * factorial(n - 1)
|
||||
}
|
||||
|
||||
// testing
|
||||
console.log(factorial(4))
|
||||
console.log(factorial(15))
|
||||
console.log(factorial(0))
|
Reference in New Issue
Block a user