-
Notifications
You must be signed in to change notification settings - Fork 0
/
factorial.js
29 lines (19 loc) · 848 Bytes
/
factorial.js
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
// * ---------------------------------------------
// * Interview Question
// * ---------------------------------------------
// ? write a fumction that takes a non negative integer number as input and returns the factorial of that number. The factorial of a non negative integer n, denoted as n!, is the product of all positive integers less than or equal to n. For example, 5! = 5 * 4 * 3 * 2 * 1 = 120.
function factorial(n) {
// ! Solution 1: using recursion
if(n == 0 || n == 1) return 1;
return n * factorial(n - 1);
// ! Solution 2: using for loop
// ? check n is negative or not
// let fact = 1;
// ? calculate the factorial of n
// for (let i = 1; i <= n; i++) {
// fact *= i;
// }
// return fact;
}
console.log(factorial(0)); // output: 1
console.log(factorial(5)); // Output: 120