-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactorialFinder.js
More file actions
38 lines (29 loc) · 849 Bytes
/
Copy pathfactorialFinder.js
File metadata and controls
38 lines (29 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* -------------------------------------------------------
* Programming Question : Factorial Finder
* -------------------------------------------------------
**/
// Q. Write a function factorial that takes a non-negative integer num as input and returns its factorial. The factorial of non-negative integer n, denoted as n!, is the product of all positve integers less than or equal to n. The factorial of 0 is defined as 1.
//constraint
//?
//?
//?
//?
function factorial(num) {
//using recursive way
if (num == 0 || num == 1) {
return 1
} else {
return num * factorial(num - 1);
}
// using for loop
let fact = 1;
for(let i=1;i<=num;i++){
fact = fact*i;
}
return fact
}
console.log(factorial(5));
console.log(factorial(-3));
console.log(factorial(0));
console.log(factorial(1));