From 39244e365b9a49aaeef87a742896e02997ab0b7b Mon Sep 17 00:00:00 2001 From: koddi-volenday Date: Tue, 4 Oct 2022 20:34:46 +0800 Subject: [PATCH] feat: add javascript factorial of a number algorithm --- Javascript/Numbers/factorial.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 Javascript/Numbers/factorial.js diff --git a/Javascript/Numbers/factorial.js b/Javascript/Numbers/factorial.js new file mode 100644 index 0000000..d0e393c --- /dev/null +++ b/Javascript/Numbers/factorial.js @@ -0,0 +1,10 @@ +// factorial method returns the factorial of a number +const factorial = (n) => { + // firstly, check that input is a number or not. + if (typeof n !== 'number') { + return 'not a number.' + } + return n <= 1 ? 1 : n * factorial(n - 1) +} + +console.log(factorial(5))