-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
66 lines (46 loc) Β· 1.29 KB
/
example.js
File metadata and controls
66 lines (46 loc) Β· 1.29 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// Types of Recursion :
// 1. Tail Recursion
// 2. Head Recursion
// 3. Tree Recursion
// 4. Indirect Recursion
// 5. Nested Recursion
// function tail(n){ // recursive function
// if(n > 0 ){
// console.log(n); //
// tail(n-1); // recursive call
// }
// }
// tail : when a recursive function call itself and that recursive call is the last statement , then it is called as "Tail Recursion".
// Time complexity : O(n);
// Space complexity : O(n);
// tail(3);
// output : 3 2 1
// function head(n){
// if(n > 0 ){
// head(n-1);
// console.log(n);
// }
// }
// head(3);
// output : 1 2 3
// Head recursion : it is a function that doesn't have to perform any operation & process at the time of calling. It has to do all the operations and process at the time of returning so these functions are called "Head recursions";
// Time complexity : O(n);
// Space complexity : O(n);
// Recursion vs loop
// Recursion : Ascending and descending
// Loop : Ascending
// function factorial(n){
// let fact = 1;
// for(let i = 1 ; i<=n ; i++){
// fact *= i;
// }
// return fact;
// }
// console.log(factorial(3));
function factorial(n){
if(n > 0){
return factorial(n-1) * n;
}
return 1;
}
console.log(factorial(3));