| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,17 @@ | |||
| 1 | + function factorial(n) { | ||
| 2 | + if (n < 0 ) throw new Error('Argument must be a positive integer.'); | ||
| 3 | + | ||
| 4 | + let result = 1; | ||
| 5 | + | ||
| 6 | + for (let i = 2; i <= n; i++) { | ||
| 7 | + result = result * i; | ||
| 8 | + } | ||
| 9 | + | ||
| 10 | + return result; | ||
| 11 | + } | ||
| 12 | + | ||
| 13 | + const inputs = [4, 6, 8, 2, -1]; | ||
| 14 | + | ||
| 15 | + inputs.forEach(n => { | ||
| 16 | + console.log(`Factorial of ${n}: ${factorial(n)}`); | ||
| 17 | + }); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,13 @@ | |||
| 1 | + function factorial(n) { | ||
| 2 | + if (n < 0 ) throw new Error('Argument must be a positive integer.'); | ||
| 3 | + | ||
| 4 | + if (n === 0 || n === 1) return 1; | ||
| 5 | + | ||
| 6 | + return n * factorial(n - 1); | ||
| 7 | + } | ||
| 8 | + | ||
| 9 | + const inputs = [4, 6, 8, 2, -1]; | ||
| 10 | + | ||
| 11 | + inputs.forEach(n => { | ||
| 12 | + console.log(`Factorial of ${n}: ${factorial(n)}`); | ||
| 13 | + }); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments