From cd7111a015d5676b17d61214ea7a51b0bbc41829 Mon Sep 17 00:00:00 2001 From: Croustys <51267148+Croustys@users.noreply.github.com> Date: Wed, 14 Oct 2020 04:02:26 +0200 Subject: [PATCH] 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 --- Recursive/factorial.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 Recursive/factorial.js diff --git a/Recursive/factorial.js b/Recursive/factorial.js new file mode 100644 index 000000000..0b1260c30 --- /dev/null +++ b/Recursive/factorial.js @@ -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))