mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-19 01:55:51 +08:00

* 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>
17 lines
306 B
JavaScript
17 lines
306 B
JavaScript
// 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))
|