10 examples of 'factorial of a number in javascript' in JavaScript

Every line of 'factorial of a number in javascript' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
5export default function factorial(number) {
6 let result = 1;
7
8 for (let i = 2; i <= number; i += 1) {
9 result *= i;
10 }
11
12 return result;
13}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

1function factorial(num) {
2 if (num === 1) {
3 return num;
4 } else {
5 let hitung = num * factorial(num - 1);
6 console.log(hitung);
7 return num * factorial(num - 1);
8 }
9 }
3function factorialize(num) {
4 if (num<=1) {
5 return 1;//0的阶乘为0,同时也是递归出口
6 }
7 return num*factorialize(num-1);//使用递归
8}
25function factorialize(num) {
26 if (num < 2) {
27 return 1;
28 }
29 return num * factorialize(num - 1);
30}
194function factorial(num) {
195 const f = [];
196 function inner(n) {
197 if (n === 0 || n === 1) {
198 return 1;
199 }
200 if (f[n] > 0) {
201 return f[n];
202 }
203 return f[n] = inner(n - 1) * n;
204 }
205 return inner(num);
206}
2function factorial(a) {
3 a = parseInt(a);
4 if (a <= 0)
5 return 1;
6 else
7 return a * factorial(a - 1);
8}
11function factorial(n) {
12 if (n < 1) {
13 return 1;
14 } else {
15 return n * factorial(n - 1);
16 }
17}
4function unfactorial(num) {
5 var d = 1;
6 while (num > 1 && Math.round(num) === num) {
7 d += 1;
8 num /= d;
9 }
10 if (num === 1) return d + "!";
11 else return "NONE";
12}
335function factorial(n) {
336 if (n == 0) {
337 return 1;
338 }
339 f = libs.BigInteger.BigInteger.ONE;
340 for (var i=1; i<=n; i++) {
341 f = f.multiply(new libs.BigInteger.BigInteger(i));
342 }
343 return f;
344}
15function factorial(n){
16 if ((n == 0) || (n == 1))
17 return 1;
18 else
19 return (n * factorial(n - 1));
20}

Related snippets